Jump to content


Is my script valid?


2 replies to this topic

#1 Snot

    Young Padawan

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:Florida

Posted 29 November 2007 - 07:13 PM

Thanks for taking a look and giving advice


Quote

<script type="javascript"><!--

browserName = navigator.appName;

browserVer = parseInt ( navigator.appVersion );



version = "n2";

if ( browserName == "Netscape" && browserVer >= 3 ) version = "n3";

if ( browserName == "Microsoft Internet Explorer" && browserVer >=4 ) version = "e4";



if ( version == "n3" || version == "e4" )

{



about_on = new Image ( 134, 94 );

about_on.src = "menuimages/about_on.gif";

products_on = new Image ( 72, 69 );

products_on.src = "menuimages/products_on.gif";

news_on = new Image ( 134, 70 );

news_on.src = "menuimages/news_on.gif";

links_on = new Image ( 134, 94 );

links_on.src = "menuimages/links_on.gif";

guestbook_on = new Image ( 134, 94 );

guestbook_on.src = "menuimages/guestbook_on.gif";

email_on = new Image ( 134, 70);

email_on.src = "menuimages/email_on.gif";





about_off = new Image ( 134, 94 );

about_off.src = "menuimages/about_off.gif";

products_off = new Image ( 134, 108 );

products_off.src = "menuimages/products_off.gif";

news_off = new Image ( 134, 70 );

news_off.src = "menuimages/news_off.gif";

links_off = new Image ( 134, 94 );

links_off.src = "menuimages/links_off.gif";


guestbook_off = new Image ( 134, 108 );

guestbook_off.src = "menuimages/guestbook_off.gif";

email_off = new Image ( 134, 70 );

email_off.src = "menuimages/email_off.gif";






}



function button_on ( imgName )

{

if ( version == "n3" || version == "e4" )

{

butOn = eval ( imgName + "_on.src" );

document [imgName].src = butOn;

}

}



function button_off ( imgName )

{

if ( version == "n3" || version == "e4" )

{

butOff = eval ( imgName + "_off.src" );

document [imgName].src = butOff;

}

}

// --></script>


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 November 2007 - 11:26 PM

There is no way to write "valid" javascript. It is what it is, and it either will work or it won't.

If it works, assume it's valid. If not, then describe what problems you're having and somebody here may be able to help.

#3 Wildhoney

    Young Padawan

  • Members
  • Pip
  • 31 posts

Posted 02 December 2007 - 04:14 PM

There is in fact valid code in Javascript, but with Internet Explorer's methods of doing things you can't always expect your Javascript code to work even if it adheres to W3C standards. Before W3C standardised Javascript, it was complete chaos with every browser dreaming up their new ways of doing things, and as you may well know, Internet Explorer still does that to this very day.

As for your code, if you wish to make it more, as you say, valid, then you will want to be using the W3C approach when calling properties of elements, such as in your code you simply reference an image's SRC attribute with .src, whereas W3C states that you must use getAttribute() and setAttribute().

Edited by Wildhoney, 02 December 2007 - 04:15 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users