0

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?

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I try to put the code that you provide in the chrome console and I put in the script.src = console.log("text") but when I take a look at the index page nothing was changed any idea why?btw when I do document.getEle...how should I refer to the index.html ?
the script src is to link to an external javascript file, you cant put inline javascript in it
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.