473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.write issue

Hi,

I have the following script:

-----------------------------------------------------------------------------------
<script type="text/javaScript">
<!--
document.write( '<div id=hello1>Hello 1</div>');
document.write( '<div id=hello2 style="display: none;"><script
src="test.js">< \/script></div>');
//-->
</script>

where test.js contains one line: document.write( "Hello2");
-----------------------------------------------------------------------------------

The intent is to only show the first div and hide the sencond div until late
to display if needed.
The result, however, shown both div contents.

If I change the script to:
-----------------------------------------------------------------------------------
<script type="text/javaScript">
<!--
document.write( '<div id=hello1>Hello 1</div>');
document.write( '<div id=hello2
style="display: none>document.w rite("Hello2"); (</div>');
//-->
</script>
-----------------------------------------------------------------------------------
It works fine.

If I further change it to:
-----------------------------------------------------------------------------------
<script type="text/javaScript">
<!--
document.write( '<div id=hello1>Hello 1</div>');
//-->
</script>

<script type="text/javaScript">doc ument.write('<d iv id=hello2
style="display: none>');</script>
<script type="text/javaScript">doc ument.write('<s cript
src="test.js">< \/script>');</script>
<script type="text/javaScript">doc ument.write('</div>');</script>
-----------------------------------------------------------------------------------
it also works fine.

Any help is highly appreciated.

Sean


Jul 23 '05 #1
12 2839
In article <msAee.1237937$ 6l.256275@pd7tw 2no>, sn********@hotm ail.com
enlightened us with...
Hi,

I have the following script:


Out of curiousity, why are you using the rather outdated document.write to
add new DOM elements?
Do you need to support old browsers?

You can add script elements and DIVs with DOM methods.

That said, I seem to recall a problem with document.write and writing the tag
"</script>"
I think you need to split it, like
document.write( "</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?

--
--
~kaeli~
When two egotists meet, it's an I for an I.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
kaeli wrote:
In article <msAee.1237937$ 6l.256275@pd7tw 2no>, sn********@hotm ail.com
enlightened us with...
Hi,

I have the following script:

Out of curiousity, why are you using the rather outdated document.write to
add new DOM elements?
Do you need to support old browsers?

You can add script elements and DIVs with DOM methods.

That said, I seem to recall a problem with document.write and writing the tag
"</script>"
I think you need to split it, like
document.write( "</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?


Exactly so. Cf. David Flanagan's "JavaScript , the Definitive Guide"
12.2.1.2 [p.214, 3rd Ed.], [p.188, 4th Ed.]

"...[T]he HTML parser makes make no attempt to understand your
JavaScript code, and if it sees the string "</script>" in your code,
even if it appears within quotes, it assumes that it has found the
closing tag of the currently running script."

/Craig Jurney
Jul 23 '05 #3
Craig Jurney wrote:
kaeli wrote:


(snip)
That said, I seem to recall a problem with document.write and writing the tag "</script>"
I think you need to split it, like
document.write( "</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?


Exactly so.


(snip)

Exactly not. This:

document.write( '<div...><scrip t src="test.js">< \/script></div>*');

....will prevent the closing tag from being parsed just as effectively
(and more compactly).

No idea what the issue is here but it seems like a pretty obscure one.

Jul 23 '05 #4
Thanks for the reply.

I have an escape for the script tag: <\/script> to avoid the problem you
mentioned but still have the wrong result.

Could you show an example of DOM method you mentioned?

Sean

"kaeli" <ti******@NOSPA M.comcast.net> wrote in message
news:MP******** *************** *@nntp.lucent.c om...
In article <msAee.1237937$ 6l.256275@pd7tw 2no>, sn********@hotm ail.com
enlightened us with...
Hi,

I have the following script:


Out of curiousity, why are you using the rather outdated document.write to
add new DOM elements?
Do you need to support old browsers?

You can add script elements and DIVs with DOM methods.

That said, I seem to recall a problem with document.write and writing the
tag
"</script>"
I think you need to split it, like
document.write( "</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?

--
--
~kaeli~
When two egotists meet, it's an I for an I.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #5
In article <l2Pee.1250285$ 8l.914640@pd7tw 1no>, sn********@hotm ail.com
enlightened us with...
Thanks for the reply.

I have an escape for the script tag: <\/script> to avoid the problem you
mentioned but still have the wrong result.

Could you show an example of DOM method you mentioned?


Sure.
I already have one here. Removes / adds a script element as needed for some
validation routines.
http://www.ipwebdesign.net/kaelisSpa...alidation.html

DIV is pretty similar, depending on what you want to do with it.

--
--
~kaeli~
Going to church doesn't make you a Christian any more than
standing in a garage makes you a car.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6
RobB wrote:
Craig Jurney wrote:
kaeli wrote:

(snip)

That said, I seem to recall a problem with document.write and
writing the tag
"</script>"
I think you need to split it, like
document.wri te("</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?


Exactly so.

(snip)

Exactly not.


No, Exactly.
This:
document.write( '<div...><scrip t src="test.js">< \/script></div>*');


That is not the code that was posted. It is the solution to the problem
though.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #7
Anytime you use a document.write, it is a good idea to escape-backslash
all closing tags contained within the document.write. Such tags may
include <\/a>, <\/p>, <\/div> etc. Usually not backslashing causes no
trouble other than making the W3C validator grumble when it sees it.
However, in a few cases, such as the <\/script> being discussed in this
thread, you can have real problems that can be very difficult to find.
I have wasted enough time in the past running down hard-to-find errors
that were caused by the lack of backslash to make me always backslash
closing tags in document.write althogh you often can get away without
doing so. On the other hand, I have never had a problem that was caused
by backslashing closing tags in a document.write.

Jul 23 '05 #8
Anytime you use a document.write, it is a good idea to escape-backslash
all closing tags contained within the document.write. Such tags may
include <\/a>, <\/p>, <\/div> etc. Usually not backslashing causes no
trouble other than making the W3C validator grumble when it sees it.
However, in a few cases, such as the <\/script> being discussed in this
thread, you can have real problems that can be very difficult to find.
I have wasted enough time in the past running down hard-to-find errors
that were caused by the lack of backslash to make me always backslash
closing tags in document.write althogh you often can get away without
doing so. On the other hand, I have never had a problem that was caused
by backslashing closing tags in a document.write.

Jul 23 '05 #9
Randy Webb wrote:
RobB wrote:
Craig Jurney wrote:
kaeli wrote:

(snip)

That said, I seem to recall a problem with document.write and


writing the tag
"</script>"
I think you need to split it, like
document.wri te("</scr"+"ipt>");
Something about the parser interpreting it as a real </script>...?
Exactly so.

(snip)

Exactly not.


No, Exactly.
This:
document.write( '<div...><scrip t src="test.js">< \/script></div>*');


That is not the code that was posted. It is the solution to the

problem though.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

You're correct. This:
document.write( '<div...><scrip t src="test.js">< \/script></div>*');


"is not the code that was posted..."

I did this thing...called "abbreviati on".

*This* is the code posted originally (with closing script tags as
strings):

[1]

<script type="text/javaScript">
<!--
document.write( '<div id=hello1>Hello 1</div>');
document.write( '<div id=hello2 style="display: none;"><script
src="test.js">< \/script></div>*');
//-->
</script>

[2]

<script type="text/javaScript">
<!--
document.write( '<div id=hello1>Hello 1</div>');
//-->
</script>
<script type="text/javaScript">doc umen*t.write('< div id=hello2
style="display: none>');</scrip*t>
<script type="text/javaScript">doc umen*t.write('< script
src="test.js">< \/script>');</s*cript>
<script type="text/javaScript">doc umen*t.write('</div>');</script>

As the OP noted:
I have an escape for the script tag: <\/script> to
avoid the problem you mentioned [..]


What are you talking about?

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2210
by: nickolausp | last post by:
When embedding a JavaScript document.write within a JavaScript document.write in Netscape 4.x (example below), the text is written out of logical order. <script type="text/javascript"> <!-- document.write('<script type=\"text/javascript\">document.write(\'111\');<\/script>222'); document.write('333'); //-->
1
3242
by: Kevin Potter | last post by:
We have an application that has been running on IIS4 and IIS5 for quite some time, without problem We're now migrating to IIS6 (windows/2003), and have run into a what might? be a Javascipt problem/question... The snippet of code in question is: <script language="JavaScript"><!-- document.write("<script src='/fp/"+includename+"'></script>"); // -->></script>
2
2186
by: Bruce | last post by:
I have sucessfully used document.write in a new window, but I now wish to write to an already open & framed window. How do I do it. Please show example, as I have trouble getting my head around JS. TIA Bruce
13
9605
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
4
2944
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400, like DOS, uses memory-mapped display, two bytes per character (one char byte and one attribute byte). We can get the screen offset allright, and I've written a javascript which does the math to convert the offset into row/col (i.e. left, top)...
11
3096
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a non-trivial exercise. inserting '&lt;' and '&gt;' causes the browser to write the text to the page as literal text rather than as the intended script element. Using escape codes seemed to work (makes it standard compliant) but the text is not written to...
2
2726
by: allan.s.palmer | last post by:
Hello, I have an asp.net pop up that is generating a Word document real time and sending it to the user by sending the appropriate response headers. When it is run on our development environment it works fine in both IE6 and IE7. We recently moved this to our UAT environment and it works fine for IE7, but IE6 users are getting the option to only download or save the .aspx page, not the generated .doc file. Has anyone run into an...
5
2963
by: SuneR | last post by:
Hi, I am having problems figuring out how to make Firefox behave, and output the HTML I want it to output. The thing I want done is actually quite simple. I have a <labeltag, and inside it, I have a script tag, that document.write's some HTML. Like this:
1
6503
by: raviviswanathan.81 | last post by:
Hello, So we have a webmaster who sets document.domain to some domain. After that, we try to create and inject text inside an iframe by getting the iframeID.contentDocument (or iframeID.contentWindow.document for MSIE). This results in an 'access denied' issue in MSIE (No problem in Mozilla). Note that if there is document.domain initialization before this iframe creation/content injection, there is no problem and all works well.
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.