External external external external script external appearance

I support an outdated javascript application that has its components broken into 4 JS files.

These are "Default.aspx", "set1.aspx", "set2.aspx" and "set3.aspx". ASPX pages write compressed JS from several (all kinds) source files belonging to their respective set and set the content type header to "text / javascript".

An application is invoked by adding a reference to the first set and creating the main record object.

<script src="/app/default.aspx" type="text/javascript"></script> <script type="text/javascript> var ax; // <body onload="OnLoad()"> function OnLoad() { ax = new MyApp(document.getElementById("axTargetDiv")); } </script> 

At the end of the first set of scripts (default.aspx), the following exact code is indicated:

 function Script(src) { document.write('<script src="' + src + '" type="text/javascript"></script>'); } Script("set1.aspx?v=" + Settings.Version); 

Which loads the second set of scripts (set1.aspx). And it works without errors in all major browsers (IE6-8 Firefox Safari Opera Chrome).

However, since I was working on this script for a quiet time, I would like to simplify function calls in many places and mistakenly introduced the above script function, resulting in the following code:

 document.write('<script src="set1.aspx?v=' + Settings.Version + '" type="text/javascript"></script>'); 

That when testing on the test page now produces the following error in all browsers:

 MyApp is not defined. 

This happens on the line: ax = new MyApp(... as a Visual Studio JS debugger, and Firebug reports this.

I tried various methods in the first 4 answers sent to this question to no avail. The only thing that will allow MyApp to load successfully is to simply add the actual "add script" code to the function (i.e. the document.write('script') ):

If I put the document.write line inside the function, it works, otherwise it is not. What's happening?

Separation and / or escaping of script text does not work.

0
javascript
source share
4 answers

To see the problem, look at this top line in the script element:

 <script type="text/javascript"> document.write('<script src="set1.aspx?v=1234" type="text/javascript"></script>'); </script> 

So, the HTML parser comes in and sees the opening tag <script>. Inside the <script> parsing of the normal <tag is disabled (in terms of SGML, the element has the contents of CDATA). To find where the script block ends, the HTML parser searches for the corresponding close </script> tag.

The first one he finds is inside the string literal. An HTML parser cannot know that it is inside a string literal, because HTML parsers do not know anything about JavaScript syntax, they only know about CDATA. So you actually say:

 <script type="text/javascript"> document.write('<script src="set1.aspx?v=1234" type="text/javascript"> </script> 

That is, an open string literal and an incomplete function call. This leads to JavaScript errors, and the desired script tag is never written.

A common attempt to solve a problem:

 document.write('...</scr' + 'ipt>'); 

This is still technically incorrect (and will not be verified). This is because, in SGML, the character sequence that ends with the CDATA element is not actually "</tagname>, but simply" </ is the sequence that is still present in the line above. Browsers, as a rule, are more forgiving and in practice this will allow.

Probably the best solution is to exit the sequence. There are several possibilities, but the simplest is to use JavaScript string literal scripts ('\ xNN'):

 document.write('\x3Cscript src="set1.aspx?v=1234\x26w=5678" type="text/javascript"\x3E\x3C/script\x3E'); 

The above excludes all the '<,'> and '& characters, which not only stops the "</ sequence appearing in the line, but also allows you to insert it into the XHTML script block without causing errors.

(There is no such thing as a CDATA element in XHTML, so these characters will have the same meaning as in the normal content, and the string '<script>' inside the script block will actually create a nested script element! You can resolve <> & in the block XHTML script using the <[CDATA [] section, but this is a bit ugly and it is usually best to avoid using these characters in an inline script.

+5
source share

1) Make sure you are not trying to link to MyApp before the script is included on your page.

2) Try breaking the word "script" in your built-in bootloader like this:

 <script type="text/javascript"> document.write('<scr' + 'ipt src="set1.aspx?v=1234" type="text/javascript"></scr' + 'ipt>'); </script> 

Alternatively, use this syntax, which I borrowed from Google Analytics code and was able to successfully use:

 <script type="text/javascript"> document.write(unescape("%3Cscript src='set1.aspx?v=1234' type='text/javascript'%3E%3C/script%3E")); </script> 
+1
source share

You can also try:

 var script = document.createElement("script"); script.src = "set1.aspx?v=1234"; script.type = "text/javascript"; document.getElementsByTagName("head")[0].appendChild(script); 

Steve

+1
source share

If you can use jQuery, you can use the following:

 $.getScript("set1.aspx?v=1234"); 

Loads the script into the global javascript context. Make sure that the text / javascript text is specified for the response contenttype parameter.

Hope this helps ...

0
source share

All Articles