JavaScript Change attribute HTML code before append and run

I wrote this html code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>untitled</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
    <script src="http://mapmap.gigfa.com/loader.js" async defer></script>
</body>
</html>

loader.js load from browser and it create script tag with createElement , then set src to alert.js, then appendChild append it to head tag. see source

No Problem. that is fine


I want change src attribute, before appendChild run!

For example, i want change alert.js to alert2.js before it load, i want hook appendChild and if src equal to alert.js, stop it and change src to alert2.js ...

I wrote this code and wrap createElement,

document.createElement = function(create) {
    return function() {
        var ret = create.apply(this, arguments);
        if (ret.tagName.toLowerCase() === "script") {
          console.log(ret.getAttribute("src"));
        }
        return ret;
        };
}(document.createElement)

but this code return undefined

javascripthtmlwrapappendchild

Answers

comments powered by Disqus