Jump to content

start javascript function on page open


ilyakar

Recommended Posts

hi, im familiar with the onload command which i can add to the body tag as soon as the page loads, but is there a way that i can start a javascript function as soon as the website opens?the reason i want to know is because i want to use the innerhtml thing to replace some content as soon as the page starts loading.thanks.

Link to comment
Share on other sites

You do NOT want to "use the innerhtml thing to replace some content as soon as the page starts loading." If you try to write into the document when the page begins loading, the places you want to write to will not yet exist.What you CAN do is place a small script in the exact location in your body where you want the content to appear. Use document.write to write out the content.ORYou can place a small script immediately AFTER the tags that create the element whose content you want to modify. That might look like this:

<div id="mydiv"></div><script type="text/javascript">   var el = document.getElementById("mydiv");   el.innerHTML = getMyData();</script>

getMyData() would be the custom function you're describing. I'm assuming it returns the text you want to write into your element. It doesn't have to do that, and you don't even need a separate function if you simply want to put all the code in the little script.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...