Document.write (<script>) throws Unexpected token "ILLEGAL"

Before asking, I have searched Stack Overflow many times, and I google it a thousand times. Any other case that I have ever seen has helped me.

Go to my problem:

I am trying to use the following script in my code:

<script type='text/javaScript'> document.write("<script src='/feeds/posts/default/-/"+hom_cat1+"?max-results=1&orderby=published&alt=json-in-script&callback=showhomeposts1'></script>"); </script> 

But I use Blogger, and it does not correctly define my code (note the red script closing tag):

note the red script closing tag

However, I cannot save the template. Therefore, I am trying to use the conversion of my code into HTML objects using this website . When I code, put this in your template and save, I get:

 Uncaught SyntaxError: Unexpected token ILLEGAL 

Here's the encoded string I'm trying to use:

 &lt;script type='text/javaScript'&gt;document.write(&quot;&lt;script src='/feeds/posts/default/-/&quot;+hom_cat1+&quot;?max-results=1&amp;orderby=published&amp;alt=json-in-script&amp;callback=showhomeposts1'&gt;&lt;/script&gt;&quot;);&lt;/script&gt; 

What can I do to solve my problem?

+7
javascript html-entities html-escape-characters
source share
4 answers

The problem is that the line passed to document.write includes </script> characters, which ends with the premature end of the script element from which document.write is called.

The </script> characters cannot appear anywhere in the script since the HTML parser cannot distinguish this from the actual </script> .

Instead, you can try something like this:

 document.write("<script src='...'></scr" + "ipt>"); 

Or as mentioned in the comments:

 document.write("<script src='...'><\/script>"); 

Another option is to use the DOM API to create a script element and paste it into the document. The other answers here give some suggestions for this, but there are potential implementation problems (e.g. document.body.appendChild will throw a TypeError if you try to call it from head ). Something like this would be more reliable:

 (function() { var s = document.getElementsByTagName('script')[0]; var script = document.createElement('script'); script.src = 'http://something.com'; s.parentNode.insertBefore(script, s); }()); 

In addition, type='text/javaScript' ; use text/javascript or omit the type attribute.

+19
source share

You write

 <script type='text/javaScript'> document.write("<script src='/feeds/posts/default/-/"+hom_cat1+"?max-results=1&orderby=published&alt=json-in-script&callback=showhomeposts1'></script>"); </script> 

So the output will be the script tag inside the script tag, so your output will be

 &lt;script type='text/javaScript'&gt;document.write(&quot;&lt;script src='/feeds/posts/default/-/&quot;+hom_cat1+&quot;?max-results=1&amp;orderby=published&amp;alt=json-in-script&amp;callback=showhomeposts1'&gt;&lt;/script&gt;&quot;);&lt;/script&gt; 

Instead, use only document.write ("");

or

 var script = document.createElement( 'script' ); script.type = 'text/javascript'; script.src = url; $("#someElement").append( script ); 
+4
source share

The best way to download js asynch is as follows

 function loadScript () { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'feeds/posts/default/your.js' document.body.appendChild(script); } window.onload = loadScript; 

or if you do not want any function, you can use a direct call

 var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'feeds/posts/default/your.js' document.body.appendChild(script); 

As suggested by google also https://developers.google.com/maps/documentation/javascript/tutorial?hl=en#asynch

+2
source share

if you are on Django and you have a similar problem: try the following:

 <script src="{% static 'my/path/to/script.js' %}" ></script> 
0
source share

All Articles