Why Split the ≪Script≫ Tag When Writing It With Document.Write()

Why split the script tag when writing it with document.write()?

</script> has to be broken up because otherwise it would end the enclosing <script></script> block too early. Really it should be split between the < and the /, because a script block is supposed (according to SGML) to be terminated by any end-tag open (ETAGO) sequence (i.e. </):

Although the STYLE and SCRIPT elements use CDATA for their data model, for these elements, CDATA must be handled differently by user agents. Markup and entities must be treated as raw text and passed to the application as is. The first occurrence of the character sequence "</" (end-tag open delimiter) is treated as terminating the end of the element's content. In valid documents, this would be the end tag for the element.

However in practice browsers only end parsing a CDATA script block on an actual </script> close-tag.

In XHTML there is no such special handling for script blocks, so any < (or &) character inside them must be &escaped; like in any other element. However then browsers that are parsing XHTML as old-school HTML will get confused. There are workarounds involving CDATA blocks, but it's easiest simply to avoid using these characters unescaped. A better way of writing a script element from script that works on either type of parser would be:

<script type="text/javascript">
document.write('\x3Cscript type="text/javascript" src="foo.js">\x3C/script>');
</script>

What is the advantage of inserting a script-tag by using document.write() (for displaying individual advertsing)?

document.write can append arbitrary, even partial, incomplete and malformed HTML into document.
It is very fast, because the browser doesn’t have to modify an existing DOM structure.

A great link http://javascript.info/tutorial/document-write

Why has this script tag been split before being used in document.write()?

There aren't any. It is just cargo-culting.

There are problems with having the sequence </script> as data inside a <script> element (since it will act as an end tag and terminate that script element). That, however, is best dealt with by an escape rather then a concatenation: "<\/script>". (Best is slightly subjective, but it is shorter, uses fewer operations and is (IMO) more readable).

The concatenation is a valid alternative to it, and some people have misunderstood the need and applied it to start tags as well as end tags, and then other people have just copy/pasted the results. Hence: cargo cult.

JavaScript embedding script tags in document.write not working

Just change closing script tag inside other script tag to fool browser.

change :

actorWin.document.write('<script type=\"text/javascript\"><\/script>')

to :

actorWin.document.write('<script type=\"text/javascript\"><\/scr'+'ipt>')

Edit :

Full code :

 newWin.document.write(
"<script type='text/javascript'>" +
"function PopUpWindowMovie(url) {" +
"movieWin=window.open(url,'','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
"movieWin.focus();}" +
"function PopUpWindowActor(){" +
"actorWin=window.open('','','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
"actorWin.focus(); " +
"actorWin.document.write('Joaquin Phoenix is a great actor and a long time vegan.<br />');" +
"actorWin.document.write('<script type=\"text/javascript\">" +
"function test() {" +
"alert(\"here\");" +
"} <\/scr'+'ipt>');" + // <-- I've edited this line
"}" +
"<\/script>");
newWin.document.write("This is a MUST SEE movie: <h1>Earthlings (2005)</h1>");
newWin.document.write("<a href=\"javascript:PopUpWindowMovie('"+documentary.title+"')\">Go to see the movie info</a><br />");
newWin.document.write("<a href=\"javascript:PopUpWindowActor()\">Go to see the lead actor</a>");

Attach script tag code to dom element for execution , alternate for document.write

I was able to resolve it by below code, thought to update if someone in future is stuck on the same problem.

var ad = "whatever encoded here script code";

function decodeHtml(html) {
return $("<textarea/>").html(html).text();
}

function fetch(id, html) {
var elm = document.getElementById(id);
elm.innerHTML = html;
var scripts = elm.getElementsByTagName("script");
var scriptsClone = [];
for (var i = 0; i < scripts.length; i++) {
scriptsClone.push(scripts[i]);
}

for (var i = 0; i < scriptsClone.length; i++) {
var currentScript = scriptsClone[i];
var s = document.createElement("script");
// Copy all the attributes from the original script
for (var j = 0; j < currentScript.attributes.length; j++) {
var a = currentScript.attributes[j];
s.setAttribute(a.name, a.value);
}
s.appendChild(document.createTextNode(currentScript.innerHTML));
currentScript.parentNode.replaceChild(s, currentScript);
}
}

var decoded = decodeHtml(ad);
fetch('YourDivID',decoded);

Why is this script element broken up into multiple document.write() calls?

This might help you:

http://www.webmasterworld.com/forum91/2913.htm

Depending on the browser, the amount of other preceding javascript,
and how well-formed the overall code
is, this is done to prevent the parser
from interpreting the and
tags as executeable code
rather than as a string to be written.

I have found that it just saves headaches down the road if you do this
when using script to write script
tags.

Why does writing a script to a page destroy the body tag, leaving a white blank page?

When you write to the document after the document is initially closed by the browser, the effect is to start all over with a fresh, empty document.



Related Topics



Leave a reply



Submit