Google Answers Logo
View Question
 
Q: Javascript document.write with script ( Answered 5 out of 5 stars,   6 Comments )
Question  
Subject: Javascript document.write with script
Category: Computers > Internet
Asked by: x2zzz-ga
List Price: $4.00
Posted: 14 Feb 2003 06:43 PST
Expires: 16 Mar 2003 06:43 PST
Question ID: 161309
This Works:
<script language=javascript
SRC=http:\\www.mysite.com\cgi-bin\banner\carousel.cgi?tell=js><\script>

This does not:
<script language="javascript">
document.write("<scr"+"ipt language=javascript
SRC=http:\\www.mysite.com\cgi-bin\banner\carousel.cgi?tell=js><\scr"+"ipt>");
</script>

How do I get the second one to work?  Thanks!

x2zzz
Answer  
Subject: Re: Javascript document.write with script
Answered By: theta-ga on 14 Feb 2003 08:13 PST
Rated:5 out of 5 stars
 
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

Request for Answer Clarification by x2zzz-ga on 14 Feb 2003 08:49 PST
Neither one of those seem to work.  Instead of mysite.com, use
nbc33.com.  I prefer not to use real url's, but for testing purposes I
will in this case.

Does work:
<script language=javascript
SRC=http:\\www.nbc33.com\cgi-bin\banner\carousel.cgi?tell=js><\script>

Doesn't Work:
<script language="javascript">
document.write("<scr"+"ipt language=javascript
SRC=http://www.nbc33.com/cgi-bin/banner/carousel.cgi?tell=js><\scr"+"ipt>");
</script>

x2zzz

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
x2zzz-ga rated this answer:5 out of 5 stars
Thanks!  Great job!

Comments  
Subject: Re: Javascript document.write with script
From: javascriptessentials-ga on 14 Feb 2003 09:12 PST
 
Your problem is not the quotes. The problem is the fact that you have a
script tag inside another script tag. You will have to rewrite the
entire expression.

What exactly are you trying to do, and what do you want to achieve.
Subject: Re: Javascript document.write with script
From: javascriptessentials-ga on 16 Feb 2003 05:48 PST
 
No no no!... your solution is far too complex, indeed the optimal
solution is in fact just one tag-pair, as follows:

<script src="http://www.nbc33.com/cgi-bin/banner/carousel.cgi?tell=js"></script>

which as you can see, requires no character escaping, no multiple
script tags, and no complex string expressions. The CGI script
carousel.cgi simply writes out javascript code into the browser -
calling it as "http://www.nbc33.com/cgi-bin/banner/carousel.cgi?tell=js"
yields the JavaScript code line:

banner=('<img ....>');document.write(banner);

where <img...> is provided randomly. So, using nested <script> tags
was never the solution intended; it was always intended for a <script
src=...> which is the best and most reliable way of utilising this
script. I know which version I'd use :-)
Subject: Re: Javascript document.write with script
From: x2zzz-ga on 16 Feb 2003 10:51 PST
 
I can't do that because i'm using MY banner manager to load a script
from NBC33's banner manager resulting in a double
document.write("<script>").  It works great now, all is good.
Subject: Re: Javascript document.write with script
From: jsimmons-ga on 17 Feb 2003 02:59 PST
 
SOLUTION! SOLUTION! SOLUTION!

<script language="javascript"><!-- 
document.write("<scr"+"ipt language=javascript
SRC=http:\\www.mysite.com\cgi-bin\banner\carousel.cgi?tell=js><\scr"+"ipt>");
//--></script> 

PLEASE CONFIRM!!

Regards,
jsimmons
Subject: Re: Javascript document.write with script
From: x2zzz-ga on 17 Feb 2003 04:36 PST
 
Um, the researcher already solved it for me and I rated his/her answer
with five stars....
Subject: Re: Javascript document.write with script
From: jsimmons-ga on 17 Feb 2003 12:34 PST
 
Yup. The researcher nailed it. Kudos! -jsimmons

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy