i have a file which is executing javascript.I want it to load after some time.For eg- my script is <script>document.write("hello")</script>
and it is loading directly when i am opening the file i want it to come after some time approx 5sec. Can you help me out as i am beginner in Javascript.Please help me to do this.Thanks in advance

- 33
- 1
- 6
-
you can create a dynamic script element using `document.createElement()` – Arun P Johny May 19 '14 at 04:31
-
see http://stackoverflow.com/questions/3619484/can-i-add-javascript-dynamically-to-an-existing-script-element#answer-3620258 – Arun P Johny May 19 '14 at 04:32
-
@ArunPJohny - without seeing what the script does, your suggested duplicate is not covering OPs question in my opinion. – mplungjan May 19 '14 at 04:45
-
@ArunPJohny the duplicate is not exactly a duplicate. – halkujabra May 19 '14 at 04:57
-
@user3651145, please consider changing the example to alert("Hello"), Seems like guys are getting confused over the the intent of you question, as i see your intent is loading a script after some delay and not writing something to a document. – Prerak K May 19 '14 at 05:34
-
No confusion at all. My answer is covering both adding something to the dom safely and doing so delayed. – mplungjan May 19 '14 at 06:38
3 Answers
You can use setTimeout function like this:
setTimeout(function() {
// Do something after 5 seconds
}, 5000);
and one thing that @mplungjan mentioned in comments that when document.write executes it wipes the document and all scripts will be removed so you should not use document.write
, instead of that use some alternate way as @mplungjan posted.

- 61,834
- 16
- 105
- 160
-
Not on a script that is using document.write - not without converting the script first – mplungjan May 19 '14 at 04:33
-
-
If the script inside the script he writes is using document.write, then your script will wipe the document after 5 seconds. You need to give more information. I will convert my comment to an answer for you – mplungjan May 19 '14 at 04:38
-
if in the function body in my chunk we call document.write there is any issue?? – Ehsan Sajjad May 19 '14 at 04:41
-
Yes, the first document.write performed after page load will completely wipe the page with all scripts in it too – mplungjan May 19 '14 at 04:46
-
ok now i got your point document.write will wipe the scripts on the page – Ehsan Sajjad May 19 '14 at 04:50
You cannot document.write after the page has loaded. If you do, the page is wiped.
It is quite easy to change the script to execute later. For example wrap the contents in a function and change any document.write inside it (show us the code please) to string concatenations
function concatIt() {
var text = "";
text += "some string previously written with document.write";
text += "some string previously written with document.write";
text += "some string previously written with document.write";
return text; // send text to calling statement
}
window.onload=function() { // when page has loaded
setTimeout(function() {
document.getElementById("somecontainer").innerHTML=concatIt();
},10000); // insert after 10 seconds
}

- 169,008
- 28
- 173
- 236
You can use -
setTimeout(function() {
document.getElementsByTagName('body')[0].appendChild('<script>document.write("hello")</script>');
}, 5000);
Based on comments-
As a suggestion, you should not be adding such a script after the document loads. This is because if you use 'document.write('something')'
, it will remove the previous content in the document and just write 'something'
into it.

- 2,844
- 3
- 25
- 35
-
-
@mplungjan The OP wants to load a script after some time, I told him how to do so. If the OP wants to load something that wipes the document, then it should be his/her own concern. That said, I don't think - if the OP loads the same script through some other method, the document will be retained. Additionaly, you should take a better look at what has been asked and then downvote the answers. – halkujabra May 19 '14 at 05:25
-
Please do not defend a poor suggestion with "if OP, who admits to be a noob, wants to dig his own grave, so be it" - what you posted is not answering the question as it wa intended unless OP wants to DOS you. What do you mean by the last sentence? – mplungjan May 19 '14 at 05:30
-
@mplungjan As I said, I just answered what was asked. What is it that has not been answered properly? Last statement meant - Please read the question and answer both carefully. Meanwhile, I have updated because I don't want OP to dig his own grave. And what is 'DOS'? – halkujabra May 19 '14 at 05:38
-
1