Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FIrefox - Variable Scope problem

Status
Not open for further replies.

NoCoolHandle

Programmer
Apr 10, 2003
2,321
US
Hi.

I have a page that has two seperate script blocks. One contains variables I need to use in the other.

This works in IE 5,6 - Safari Windows and Mac, - Opera Windows and Mac.

In firefox it doesn't..
Basic idea below
Code:
[blue]<[/blue][maroon]script[/maroon] [red]type[/red][maroon]=[/maroon][blue]"text/javascript"[/blue][blue]>[/blue]
  [blue]var[/blue] x = [maroon]"test"[/maroon];
[blue]</[/blue][maroon]script[/maroon][blue]>[/blue]
[blue]<[/blue][maroon]script[/maroon] [red]type[/red][maroon]=[/maroon][blue]"text/javascript"[/blue]>
  [green]// this works in everthing except firefox[/green]
  [blue]alert[/blue](x);
[blue]<[/blue][maroon]script[/maroon][blue]>[/blue]
 
Hi

Code:
<script type="text/javascript">
  var x = "test";
</script>
<script type="text/javascript">
  // this works in everthing except firefox
  alert(x);
<[COLOR=red pink]/[/color]script>

Feherke.
 
Sorry I forgot to add the back slash when i was typing the code in the editor window. In my actual page it was there and but it still didn't work in Firefox..

My guess is that the code I had above wouldn't even run in IE.

IF you can get the example you put there to work, I would love to know. (It Still does't work for me - in firefox)

Rob
 
First thing is to copy and paste your code here, as it actually is. That will avoid chasing down problems like feherke did, and missing problems that are actually in the original.

Lee
 
Are you defining the variable as a global variable in the first script block?

As Lee says, let's see the real code rather than transcribed code which may or may not be correct.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Sorry.. I need to dig deeper. I must have missed a semicol somewhere.

 
Hi

NoCoolHandle said:
I must have missed a semicol somewhere.
I am not so sure. Personally I spare the semicolons ( ; ) since years without visible downside. Unless you have multiple statements in the same line, your problem probably lies elsewhere.

( Yes, I know, that is bad habit. The end-of-line is not the same on all systems while the semicolon is. But that may cause portability problems. Which I think is not the case here. )

Feherke.
 
I'm new to this forum (and to javascript running under Firefox). Did anyone actually answer this question? Short of the syntax issues he's correct. Assuming X is a "global" variable, firefox doesn't seem to let you access it from a different script block. What's up with that. Is there something simple not mentioned here ?
 
I don't get all this speculation, when all it took me to answer the question as to whether Fx is at fault was a 10-second test harness:

Code:
<html>
<head>
	<script type="text/javascript">
		var x = "test";
	</script>
	<script type="text/javascript">
		// this works in everthing except firefox
		alert(x);
	</script>
</head>

<body></body>
</html>

This works fine for me in Fx2/Win.

This probably means the OPs code has some other bug in it.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

Usually I skip testing such simple cases, but I tested this one.

Ok, let us modify abit Dan's code sample to get closer to the error.

First join the two [tt]script[/tt] sections :
Code:
<html>
<head>
    <script type="text/javascript">
        var x = "test";
        // this works in everthing except firefox
        alert(x);
    <[COLOR=red pink]/[/color]script>
</head>

<body></body>
</html>
Then remove the slash ( / ) from the closing [tt]script[/tt] tag, as it was in the original code :
Code:
<html>
<head>
    <script type="text/javascript">
        var x = "test";
        // this works in everthing except firefox
        alert(x);
    <script>
</head>

<body></body>
</html>
My conclusion is the OP had a HTML markup error caused by a typo.

allmyhandlesaretaken, what tests you made that led you to your conclusion ?

Feherke.
 
The original error is still occourning, however I don't believe it is strictly related to Firefox any more.

One issue I had was in the script block where the variable was defined, I had specified the type as "text/jscript" this Firefox HATED!!!. No other browser tested had an issue, but in Firefox "text/javascript" fixed some of the issues.

I am still suffereing with a similar error in one page, but now it is across browsers at least :)

My belief is that I have some heavy jquery stuff going on that is causing problems. Also most of the code is being generated on the server side - both for the UI and also for the code.. This definitly enhances the chances of a *^%$ up.

For what it is worth.


Rob

 
One issue I had was in the script block where the variable was defined, I had specified the type as "text/jscript"

Another reason to show us real code that has been copied from the problematic source instead of typing what you think it says - as the example above clearly shows text/javascript.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Dan, The reason I didn't is that it is rather extensive and i didn't want to waste too much of your time wading through none salent data.

For what it is worth this is still causing and error at line 93 char 20.

If you don't mind looking at it, I am still interested. It won't kill the project if I can't fix it on this page. The same code runs on every other page.

code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head><title>
	Untitled Page
</title>
   
    <!--[if lt IE 7]>
     <style type="text/css">
     div, img { behavior: url(iepngfix.htc) }
     </style>
    <![endif]-->
     <script src="js/jquery.js" type="text/javascript"></script>
    <link href="css/sitemaster.css" rel="stylesheet" type="text/css" />
    <link href="css/locations.css" rel="stylesheet" type="text/css" />
    
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/mapSupport.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/locations.js" />
  <!--[if lt IE 7]>
     <style type="text/css">
     div, img { behavior: url(iepngfix.htc) }
     </style>
    <![endif]-->
<script  type='text/javascript'>
var imgs = new Array();
imgs[0] = 'images/headimages/Agg.jpg';
imgs[1] = 'images/headimages/Aggr6.jpg';
imgs[2] = 'images/headimages/Aggre1.jpg';
imgs[3] = 'images/headimages/Aggre2.jpg';
imgs[4] = 'images/headimages/Aggre3.jpg';
imgs[5] = 'images/headimages/Aggre4.jpg';
imgs[6] = 'images/headimages/Aggre5.jpg';
imgs[7] = 'images/headimages/Aggre6.jpg';
imgs[8] = 'images/headimages/Asph1.jpg';
imgs[9] = 'images/headimages/Asph2.jpg';
imgs[10] = 'images/headimages/Asph3.jpg';
imgs[11] = 'images/headimages/Joint1.jpg';
imgs[12] = 'images/headimages/Joint2.jpg';
imgs[13] = 'images/headimages/Lab.jpg';
imgs[14] = 'images/headimages/Logis.jpg';
imgs[15] = 'images/headimages/Logis2.jpg';
imgs[16] = 'images/headimages/MillServ.jpg';
imgs[17] = 'images/headimages/MillServ2.jpg';
imgs[18] = 'images/headimages/MillServ3.jpg';
imgs[19] = 'images/headimages/Peop1.jpg';
imgs[20] = 'images/headimages/People.jpg';
imgs[21] = 'images/headimages/People3.jpg';
imgs[22] = 'images/headimages/People4.jpg';
imgs[23] = 'images/headimages/People5.jpg';
imgs[24] = 'images/headimages/People6.jpg';
imgs[25] = 'images/headimages/People7.jpg';
imgs[26] = 'images/headimages/People8.jpg';
</script>

    <script type="text/javascript">

    var img = 0;

        $(function()
        {
            $(".search").hide()
                        .show();
            $(".logo").click(function(){window.location="home.aspx"});
            $(".button").hover(function(){$(this).css("color","yellow");},function(){$(this).css("color","white")});
            $(".cntbutton").click(function(){alert("img")});
            $("#vid1").css("backgroundImage","url(images/headimages/people8.jpg)");
            $("#vid2").css("backgroundImage","url(images/headimages/people4.jpg)");
            $("#vid3").css("backgroundImage","url(images/headimages/people6.jpg)");
            $(".sub").css("visibility","visible")
                     .hide();
            $(".imgBtn").hover(function(){$("#divImaginationButton").css("color","yellow");},function(){$("#divImaginationButton").css("color","white")});
            $(".bottomMenu").click(function(){window.location=($(this).attr("href"))});
            $("#contactus").hover(function(){$(".sub").slideDown(500)},function(){});
            $(".btmMenuCont").hover(function(){},function(){$(".sub").slideUp(500)});
            setInterval("getNextImage()",3200);
        });
        function getNextImage()
        {
                var ran_unrounded=Math.random()*24;
                var ran_number=Math.floor(ran_unrounded); 
                if (img==0)
                {  
                   //alert(imgs[ran_number]);
                   $("#imgLower").fadeIn(2000);
                   $("#imgMid").fadeOut(2000);         
                   img=1;
                }
                else if(img==1)
                {
                   $("#imgMid").attr("src",imgs[ran_number]); 
                   img=2;
                }
                else if (img==2)
                {
                 //alert(imgs[ran_number]);
                   $("#imgMid").fadeIn(2000);
                   $("#imgLower").fadeOut(2000);                                      
                    img=3;
                }
                else if(img==3)
                {
                    $("#imgLower").attr("src",imgs[ran_number]);
                    img=0;
                }
        }
        
    </script>
    <link href="greybox/gb_styles.css" rel="stylesheet" type="text/css" />
 
Sorry Dan,

Try this

dev.acincorporated.com/levy/locations.aspx

All files referenced are relative to the levy directory.

 
Hi

Try to avoid the [tt]script[/tt] tag minimization :
Code:
[s]<script type="text/javascript" src="js/locations.js" />[/s]

<script type="text/javascript" src="js/locations.js"></script>
There is nothing wrong with that syntax, but as far as I read, some browsers get confused by that.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top