Hi x2zzz-ga,
The reason that the second snippet of code does not work, is that
you have not used the Javascript escape character to properly specify
the string being written by the document.write function. As a result,
Javascript incorrectly interprets the string and gives you an error.
What is the Javascript Escape character ?
+---------------------------------------+
Javascript uses the backslash(\) character as an escape character,
which, in combination with the character following it, can be used to
include special charactes in a string. For eg., if you want to have a
string containing the following text:
This is a double quote(").
It's Javascript representation will be :
str = "This is a double quote(\")."
Note that the quote that is the part of the input text, is specified
as \". If we had used a normal double quote instead, the Javascript
parser would take it as signifying the end of the string, and the code
would have resulted in an error.
Here are some of the eschape character sequences specified in
Javascript:
\b - Backspace( deletes the previous character )
\f - Form feed
\n - New line
\r - Carriage return
\t - Tab
\' - Single quote or apostrophe (')
\" - Double quote (")
\\ - Backslash (\)
For a complete list of Javascript escape sequences and their
descriptions, check out these articles :
- JavaScript Escape Character (Google Cache Link)
( http://216.239.57.100/search?q=cache:hxL0H0MtevsC:html.surfnet.nl/programmer/jstut/jsTabChars.html&hl=en&ie=UTF-8)
- Core JavaScript Guide: Values, Variables and Literals
( http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/ident.html#1008368
)
The Corrected Script
+-------------------+
The only character that needs to be escaped in your string is the
backslash(\) character. To write one backslash character, we have to
use the following escape sequence: \\
So, the corrected script is:
========== Code Begin ===========
<script language="javascript">
document.write("<scr"+"ipt language=javascript"+
"SRC=http:\\\\www.mysite.com\\cgi-bin\\banner\\carousel.cgi?tell=js>"
+ "</scr" + "ipt>");
</script>
=========== Code End =============
OR, just to make it simpler, you can use the forward slash(/)
character in your URL. This is the norm on the web, by the way. So the
corrected code will now be :
========== Code Begin ===========
<script language="javascript">
document.write("<scr"+"ipt language=javascript"+
"SRC=http://www.mysite.com/cgi-bin/banner/carousel.cgi?tell=js>"
+ "</scr" + "ipt>");
</script>
=========== Code End =============
+-----------------------------+
Hope this helps.
If you need any clarifications, just ask!
Regards,
Theta-ga
===============================
Google Search terms used:
javascript escape characters |
Clarification of Answer by
theta-ga
on
14 Feb 2003 10:38 PST
Hi x2zzz-ga,
When I pasted the original code snippets into my test HTML page,
and tried opening it in IE, I got an "Unterminated string constant"
error. This led to my fixing up your code with the escape characters,
which eliminated IE's error messages. Unfortunately, you had not
specified what problems you were facing with the code, and without a
valid site URL I was in no position to check out the code's effect.
So, I assumed that your problem was solved. Sorry about that.
:-)
However, with a better description of your problem, a working URL
to check the solution with, and javascriptessentials-ga's helpful
comment below, I believe that I have (finally!) found the answer.
The problem is indeed with the fact that you have nested script
tags in your page. You can find a Google Group discussion of this
problem here:
- Subject: Strange JavaScript/Server problem
Newsgroup: macromedia.ultradev
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=a1cjjn%24k92%241%40forums.macromedia.com&rnum=16)
This article from the November 1999 issue of Microsoft Internet
Developer also mentions this problem for ASP pages:
- MIND: Geek To Geek
( http://www.microsoft.com/mind/1199/geek/geek1199.asp )
The solution is pretty simple. All you have to do is escape(There's
that word again!!) the ending script tag, so that the interpreter is
unable to recognise that it is a script tag. The following code
snippet demonstrates this:
========== Code Begin ===========
<script language="javascript">
document.write("<scr"+"ipt language=javascript"+
" SRC=http://www.nbc33.com/cgi-bin/banner/carousel.cgi?tell=js>"+
"<\/scr"+"ipt>"); // Note the change <\/script>
</script>
=========== Code End =============
The above code snippet was tested successfully on my IE5, and
resulted in a banner ad appearing on my page. So, it should do the job
for you.
+---------------------------------+
Hope this helps.
If you need any clarifications, just ask!
Regards,
Theta-ga
===================================
Google Search terms Used:
javascript nested script tag
|