Forum Moderators: open

Message Too Old, No Replies

help with syntax

         

th1chsn

6:28 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Can someone familiar with javascript help me figure out what's wrong with the below code? What I'm trying to do is determine a page name and then show some code depending upon what the page name is.


<SCRIPT LANGUAGE="JavaScript">
var dir = location.href.substring(0,location.href.lastIndexOf('/')+1);
var url = location.href.substring(dir.length,location.href.length+1);

if (url=="page.php") {
document.write('<a href="link.php"><img src="../image.gif" align="top" border="0"></a>');}
else {
document.write('<a href="link.php">
onmouseover="image2.src='../imageon.gif';"
onmouseout="image2.src='../imageoff.gif';">
<img name="image2" src="../imageoff.gif" border="0"></a>');}
</SCRIPT>

RonPK

6:46 pm on Jan 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should return the proper file name:

var url = location.pathname.substring(location.pathname.lastIndexOf('/')+1);

lavazza

7:37 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



The document.write of the else has some syntax errors

Try


else {
document.write(
'<a href="link.php">'
+ ' <img name="image2" src="../imageoff.gif" border="0" '
+ ' onmouseover=(image2.src="../imageon.gif");'
+ ' onmouseout=(image2.src="../imageoff.gif");><\/a>');
}

th1chsn

8:17 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Thanks lavazza, that worked great and made it easier to read.

lavazza

8:51 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



You're welcome

I notice that I forgot to 'escape' the forward slashes in the img file/path (I remembered to do so for the closing A tag)

As they're within a 'string', it's unlikely that they'll be parsed as division characters... but I have a hunch that it never hurts to be a purist

else {
document.write(
'<a href="link.php">'
+ ' <img name="image2" src="..\/imageoff.gif" border="0" '
+ ' onmouseover=(image2.src="..\/imageon.gif");'
+ ' onmouseout=(image2.src="..\/imageoff.gif");><\/a>');
}

th1chsn

9:04 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



It worked without them but I'll add them in b/c it will bother me if I don't.