codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How to separate a javscript into HEAD and BODY, global variable  vanessa19 at 17:22 on Friday, March 04, 2005
 

Hi all,

I am attempting to learn something here.

I have a javscript that works fine when I put it in the BODY of my page:


var resSize

if (screen.width < 690)
{
resSize = "640x480"
}
else if (screen.width < 850)
{
resSize = "800x600"
}
else if (screen.width < 1079)
{
resSize = "1024x768"
}
else if (screen.width < 1200)
{
resSize = "1152x864"
}
else if (screen.width < 1330)
{
resSize = "1280x960"
}
else if (screen.width >= 1600)
{
resSize = "1600x1200"
}
{
document.write('<a href="thispage_' + resSize + '.html">')
}


However, I need to use this little script multiple times on the page, so, to make things more concise, I'd like to separate the script into two :

In the HEAD of my page:


var resSize

if (screen.width < 690)
{
resSize = "640x480"
}
else if (screen.width < 850)
{
resSize = "800x600"
}
else if (screen.width < 1079)
{
resSize = "1024x768"
}
else if (screen.width < 1200)
{
resSize = "1152x864"
}
else if (screen.width < 1330)
{
resSize = "1280x960"
}
else if (screen.width >= 1600)
{
resSize = "1600x1200"
}


And in the BODY of my page:


{
document.write('<a href="thispage_' + resSize + '.html">')
}


However, if I do that, nothing seems to happen at all. What am I doing wrong?

Thanks,

Vanessa

  Re: How to separate a javscript into HEAD and BODY, global variable  vanessa19 at 05:53 on Saturday, March 05, 2005
 

Sorry...figured this out myself, Just extraneous {} were causing it to fail. Thanks anyways.

Vanessa

  Re: How to separate a javscript into HEAD and BODY, global variable  tgreer at 20:15 on Monday, March 07, 2005
 

You also need to get into the practice of terminating your statements with the semicolon.


  Re: How to separate a javscript into HEAD and BODY, global variable  vanessa19 at 01:10 on Wednesday, March 09, 2005
 

Thanks! Do you mean at the end of every line?

Vanessa

  Re: How to separate a javscript into HEAD and BODY, global variable  tgreer at 02:27 on Wednesday, March 09, 2005
 

Not EVERY line, but every "statement". It's not a technical requirement, as the specification is a bit forgiving. But it's good practice.

So in your code, the "resSize =" statements should, if you're being strict, have terminal semicolons.





© Copyright codetoad.com 2001-2005