简体   繁体   中英

Add script tag to index.html during runtime

I've the following document and I want to add to it script under the title after the page load,how should I do that ?

This is my simple page

<html>
<head>
<title>Simple demo</title>
</head>
<body>
<h1>Demo</h1>
<p>
Very simple UI – just text and a link:
<a id="demoLink" href="#">Click me!</a>
</p>
</body>
</html>

The script should be very simple

<script>console.log(test) </script>

is it possible at all ? if not how should I do that via console?

After the page load, there's no major difference between "adding an script node" or "executing a piece of script". If you do want to add the <script> node in <head> , you could use the DOM API .

to load a script after page load you'd need to add it in like this:

<script>
    var script = document.createElement('script');
    script.src = "http://something.com/js/something.js";
    document.getElementsByTagName('head')[0].appendChild(script);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM