"listen " wait for tag to print
I have javascript "API" function that need a certain tag to work with.
i got problem with the positioning of the script. here is what i mean :
if the Script has been written in the head , i need to wait for the tag to get print开发者_运维问答ed to work with , what's means that i need to follow on the page and check when the tag get print and than start the function.
if the script has been written in the body after the tag got printed , i can't call the function after the tag got printed , like this :
<tag></tag> <script> startfunction(); </script>
my question is , how can i follow the tag , check if it prints , if it does start the functions , if it doesn't "listen" to the tag and wait for it to print and than start the function , *i cant wait for the whole page to load.
how to make it done?
Can't you wait for the document to finish loading? The easiest way, would be to listen for the "load" event on your window element, and trigger your code then...
I don't know if you're using some JS library like jQuery, Prototype or something else...
Here is some standard JS for handling this type of event:
if( window.addEventListener ) {
window.addEventListener('load',handlerFunction,false);
} else if( document.addEventListener ) {
document.addEventListener('load',handlerFunction,false);
}
Your handlerFunction would trigger all necessary functions, you could then do something like:
document.getElementsByTagName("tag")
And check if that code returns any elements in order to know if your tag got printed...
Does this help? Let me know.
Edit BTW, this could be a duplicate of: javascript domready?
精彩评论