Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with JavaScript within Document.write

Status
Not open for further replies.

theburton

Programmer
May 24, 2001
4
0
0
CA
I'm currently serving a header for our webpage through a javascript call to a cgi, with the following line:

<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;
The javascript-header.fcgi prints the header within the &quot;document.write('');&quot; commands. My problem is our Ad code has javascript code within it, and this doesn't seem to be working when we serve the header.

I've taken out the document.writes around the code itself, but again this doesn't work when we call the header from our webpage. It works when we call the javascript-header.fcgi program directly, but that's pretty useless as every line has document.writes around it. If anybody can make sense of my ramblings, and wishes to help, please post a reply. Thank.
 
I'm grasping at straws here but it could be something to do with nested quotes. Mise Le Meas,

Mighty :)
 
Can you show what the js code of the linked page looks like after it's generated?
- tleish
 
the javascript-header.fcgi cgi returns the following code:


blah blah blah....
document.write(' <tr valign=&quot;middle&quot;> ');
document.write(' <td colspan=&quot;3&quot; align=&quot;center&quot; bgcolor=&quot;#FFFFFF&quot; height=&quot;70&quot;> ');
document.write(' <!-- START ADCYCLE STANDARD CACHE-BUST CODE for OzDating_Top-->');
document.write('<script language=&quot;javascript&quot;>');
document.write('<!-- /* © 2001 AdCycle.com All Rights Reserved.*/ ');
document.write('var id=269;');
document.write('var jar=new Date();var s=jar.getSeconds();var m=jar.getMinutes();');
document.write('var flash=s*m+id;var cgi=\'document.write('var p=\'<a href=&quot;\'+cgi+\'/adclick.cgi?manager=adcycle.com&gid=10&layout=multi&id=\'+flash+\'&quot; target=&quot;_blank&quot;>\';');
document.write('p+=\'<img src=&quot;\'+cgi+\'/adcycle.cgi?gid=10&layout=multi&id=\'+flash+\'&quot; width=468 height=60 \';');
document.write('p+=\'border=1 alt=&quot;Click to Visit&quot;></a>\'; document.write(p); // -->');
document.write('</script>');
document.write('<noscript>');
document.write('<a href=&quot; target=&quot;_blank&quot;><img src=&quot; width=468 height=60 border=1 ALT=&quot;Click to Visit&quot;></a>');
document.write('</noscript>');
document.write('<!-- END ADCYCLE CODE -->');
document.write(' </td>');
document.write(' </tr>');
document.write(' ');
blah blah blah...

Hopefully this sheds some light on the situation.
Thanks,
 
I tried to nest JavaScript within a document.write before and I had problems with the whole nested quotes thing. As far as I know \' is not valid. \&quot; is but this does not work with single quotes. Mise Le Meas,

Mighty :)
 
Here's an example of a similiar situation I had, hope it helps.

Liz

try concatonating a variable, and then do document.write(myconcatonatedvariable)

var html

html = '<div align=&quot;center&quot;><img src=&quot;/intranet/library/icons/dir_belllogo.gif&quot; border=&quot;0&quot;><b>FILE PROPERTIES</b>'
html += '<br><a target=&quot;blank&quot; href=&quot;'
html += editablePath
html += '&quot;><b>EDIT' + strName + 'online? </b></a></div> <br>You can make changes to this file online by clicking EDIT above, modifying the file and then saving the file. Changes will appear the next time the file is opened.'
html += '<hr style=&quot;border-width:1px; border-color:#000066; border-style: thin&quot;'
html += '<br> Name:' + fileName
 
Generate first into perhaps a file or variable
 
maybe file0001.tmp etc... then delete em when ur done
 
There's a bunch of things that can't be done with this code:

1. document.write('<script language=&quot;javascript&quot;>');
document.write('</script>');

You've allready done this when you called <SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot; Essentially what you are trying to do, and it can't be done is you are embeding a javscript tag within a javascript tag.
<script language=&quot;javascript&quot;><script language=&quot;javascript&quot;></script></script>

2. document.write('<noscript>');
document.write('</noscript>');

This tag is used for those who do not have JavaScript or do not have it enabled. This tag, like the ones above, cannot be imbedded in a JavaScript tag. Besides, even if it did work, if the person didn't have the JavaScript, it wouldn't be able to use the document.write command to even write the <noscript> tag.

3. document.write('<!-- /* © 2001 AdCycle.com All Rights Reserved.*/ ');
document.write('var flash=s*m+id;var cgi=\' document.write('p+=\'border=1 alt=&quot;Click to Visit&quot;></a>\'; document.write(p); // -->');

There are some improperly escaped &quot;//&quot; or &quot;<!--&quot;, which in javascript anything after it in that line is a comment. &quot;<!--&quot; is ok in the same line if &quot;-->&quot; follows it later, but if not, then the rest of the line is commented out.

4. document.write('var flash=s*m+id;var cgi=\'Why use javascript to make javascript variables, it doesn't work this way. If dynamic variables are needed, then document.write isn't the way to go on this.
- tleish
 
Nested Javascript is no problem if you do the following:

In order to include a <script> tag inside a an existing JS script, you have to break the tag up into parts, or it will attempt to evaluate instead of simply write as a string.
Code:
document.write('<scr' + 'ipt language=&quot;javascript&quot;>');

and

document.write('</scr' + 'ipt>');
Also, You have to pay attention to your nested quotes. In the above script, you are OK so far, but I recommend you actually escape your quotes:
Code:
document.write(&quot;<scr&quot; + &quot;ipt language=\&quot;javascript\&quot;>&quot;);

and

document.write(&quot;</scr' + 'ipt>&quot;);
This is more in keeping with standardized programming methods for dealing with strings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top