user4209821
user4209821

Reputation:

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?

Upvotes: 0

Views: 1424

Answers (2)

atmd
atmd

Reputation: 7490

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>

Upvotes: 1

Jeffrey Zhao
Jeffrey Zhao

Reputation: 5023

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.

Upvotes: 0

Related Questions