0

Script old (1) :

document.write('<scr'+'ipt src="http://example.com/script.js?'+Math.random()+'" type="text/javascript" onerror="checkScriptLoaded2();" onload="scriptLoaded2=true;"></scr'+'ipt>');

Script new (2):

var sNew = document.createElement("script");
sNew.async = true;
sNew.src = "http://example.com/script.js?"+Math.random();
var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(sNew, s0);

How do I put this tag in the script 2 in order to work properly.

onerror="checkScriptLoaded2();" onload="scriptLoaded2=true;"
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

2 Answers2

0

Like so:

sNew.onerror = checkScriptLoaded2;
sNew.onload = function() {
  scriptLoaded2 = true;
};

You can learn more about this type of event handling on quirksmode.org. Basically, for most events an element supports, is as a on<eventname> property to which you can assign a function to handle it.

See the HTMLScriptElement documentation on MDN (although it doesn't list the event handlers).


Also note that

var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(sNew, s0);

will only work if there is already another existing <script> element. If the script doesn't have to appear anywhere particular then doing what MDN suggests is a bit more failsafe:

document.head.appendChild(sNew);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thank you very much for the suggestion feedback. I would give a vote, but unfortunately I do not have enough reputation. –  Dec 17 '16 at 06:53
  • That's fine. I can only tell you that you should not be using the `setAttribute*` methods. That's a step backwards. If you have a DOM element, use the DOM properties. – Felix Kling Dec 17 '16 at 06:55
0

This way.

Create attribute and add it to script element.

The createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object.

attribute.value property is used to set the value of the attribute.

element.setAttributeNode() method is used to add the newly created attribute to an element.

function checkScriptLoaded2()
{
  alert("Error in loading.")
}

var sNew = document.createElement("script");
sNew.async = true;
sNew.src = "https://example.com/script.js?"+Math.random();


sNew.setAttribute('onerror', 'checkScriptLoaded2();') // Shorthand as suggested by Felix

var att = document.createAttribute("onload"); // Elaborated way 
att.value = "scriptLoaded2=true;"
sNew.setAttributeNode(att)




var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(sNew, s0);
<div>hello</div>
Deep
  • 9,594
  • 2
  • 21
  • 33
  • Why use HTML attributes when you can use DOM properties (especially since you using the `async` and `src` DOM properties)? – Felix Kling Dec 17 '16 at 04:12
  • @FelixKling this is one of the way to achieve , however would be glad if you can provide a working sample of your answer. – Deep Dec 17 '16 at 04:14
  • Sure it's one way, but it's not very common. Maybe you can at least explain more about `createAttribute` and `setAttributeNode`. Another (more concise) way would be to use `setAttribute('onerror', '...')` btw. – Felix Kling Dec 17 '16 at 04:19
  • @FelixKling Aye Aye Captain – Deep Dec 17 '16 at 04:26