For a website I’m working on, I’ve created an embed function. This is just a call to a javascript on my server that injects some HTML on the remote server.
To make sure the embeds also get some Google juice, I’ve added a piece of html, so the embed code looks something like this:
<script type="text/javascript" src="http://myserver.com/myscript.js"></script>
<div id="embedSource">Source: <a href="myserver.com">mysite.com</a></div>
It’s easy to remove the last div with the source in it from the embed code. So I thought I’d just add a little detection script in the first “myscript.js” to detect the presence of the div with id “embedSource”.
Problem is: the javascript is always executed first, then the div is being added to the DOM and there is no way that I can add the embedSource div first.
I’ve tried to check using:
window.onload = CheckIfEmbedCodeIsComplete();
But this event is fired to early (Chrome, FF and IE). Even though for most browsers it should be enouugh (right?).
Also tried:
setTimeOut(CheckIfEmbedCodeIsComplete(), 5000);
But again, the check is being called immediately, not after 5 seconds as you would expect.
What can I do to still check if that div exists?
To answer my own question, I had to change the timeout call to this: