Forum Moderators: open

Message Too Old, No Replies

Unknown JavaScript Error

Can't figure out what it means

         

MatthewHSE

7:54 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wrote an extremely simple script, the very first "script" I've ever written without help. Unfortunately it's not working . . .

Here it is:

<script type="text/javascript">
if (window.top.location == http://mydomain.com/page.html?id=x) {
document.write('<style type="text/css">.class1 \{display: none;\} .class2 \{display: block;\}</style>);
}
</script>

The error I'm getting (using FireFox's JS console) is as follows:

missing ) after condition.

It then gives the line with the URL (line 2 here).

Where in the world would another parenthesis go? I've checked with w3schools, did a search here, looked at other javascripts, can't see what I'm doing wrong. Can anyone else spot the error? If so please point it out to me . . . I don't get it at all!

Thanks,

Matthew

klogger

9:00 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



The window.top.location needs quotes around the URL.


if (window.top.location == "http://mydomain.com/page.html?id=x") {
document.write("<style type=\"text/css\">.class1 {display: none;} .class2 {display: block;}</style>");
}

You also had the escape character '\' by the curly bracket '\{' when it should have just been '{'. Use the escape character '\' (without the quotes) to escape the "text\css" so it becomes \"text/css\"

Hope that makes sense.

Bernard Marx

9:29 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



+ If you use single inverted commas for the document.write:

document.write(' ..blah.. ')

then you don't need to escape the "s.

[..but you might want to escape /s (forward slashes) just for safety anyway]