Page 1 of 1

Javascript delay problem... help please!

Posted: Fri May 18, 2007 9:16 pm
by Citizen
If I use this:

Code: Select all

<script>
document.writeln('<p>What ever you want to write<\/p>');
</script>
In my html code, it prints fine like normal text.

But.... if I use this to create a delay before writing the text, it creates a new blank page with only the written text:

Code: Select all

<script>
setTimeout(\"document.write('<p>What ever you want to write<\/p>')\",500);
</script>

Is there any way for me to put a delay before writing the text without it creating a new page?

Thanks!

Posted: Sat May 19, 2007 4:43 am
by Ollie Saunders
Ahh that'll be DOM scripting. What you'll want to do is put your code inside

Code: Select all

window.onload = function() { /* here */ }
and use DOM traversal to find the point where you want to insert the paragraph. and then use things like

Code: Select all

document.createElement('P') and someNode.appendChild()
You really have to learn it though. However if you pick up a JavaScript framework you may find specific documentation on how to achieve this using that framework.

In addition I don't think you can use setTimeout like that. This is the correct usage

Code: Select all

function timeoutHandler()
{
   // code
}
setTimeout(timeoutHandler, 500);