Hi there, I have a site that uses the cpa lead gateway system that is activated when a user clicks through to a page.php to view a embeded video. I would like to delay the script from activating for a set time that I choose (e.g. 60 seconds or more). The code a present is placed at the top of the video.php before the first <?php tag. The code examlpe is: <script src="http://www.cpalead.com/gateway.php?pub=????" language="JavaScript" type="text/javascript"></script> Is there a simple way to add a delay function to this script, if so can you show me an example with the above script included? Many thank's for taking the time to help on this problem!
Maybe something like this? <script id="external_script" type="text/JavaScript"></script> <script> var changeSrc = function() { document.getElementById('external_script').src = 'http://www.cpalead.com/gateway.php?pub=????'; }; setTimeout(changeSrc, 3000); </script> Code (markup): Just change the 3000(this is 3 seconds) to however long you want to delay. Number of seconds * 1000. Got the idea from here: http://ajaxpatterns.org/On-Demand_Javascript
Hi "LogicFlux", Thank's for your reply, I tried out the script with your mods added, the delay did work but it reloaded the page which was then blank and just hung while appearing to be connecting to the cpa lead server! Do you have any suggestions? Many thank's netnuta
It doesn't work. I don't know why. I tried just inserting the script statically with a pub value of 1234 and it worked fine. But if I try to switch the src attribute of the tag or insert the script element dynamically it hangs, although I can see in firebug that the stuff is being loaded in the document from javascript output of the script. But it's not being rendered in the browser window. I tried this: <script> var insertScript = function() { var head = document.getElementsByTagName("head")[0]; var script = document.createElement('script'); script.id = 'cpalead-script'; script.type = 'text/javascript'; script.src = "http://www.cpalead.com/gateway.php?pub=1234"; head.appendChild(script); } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } var initScriptInsert = function () { setTimeout(insertScript, 1000); } addLoadEvent(initScriptInsert); </script> Code (markup): It hangs too. I don't know why. Loading the script through javascript dom access was just the first approach that came to my mind. Perhaps it's a flawed approach. Maybe someone knows another way.
Here's something that sort of works. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title> delay load </title> <script> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } var showDiv = function (){ document.getElementById('the-script').style.display = 'block'; }; addLoadEvent(function () { setTimeout(showDiv, 3000); }); </script> </head> <body> <div id="the-script" style="display:none;"> <script src="http://www.cpalead.com/gateway.php?pub=1234" type="text/javascript"></script> </div> </body> </html> Code (markup):
Many thank's for your input! Yes the delay does work, but the problem is if inserted into the header.php file it would make the gateway code load on all pages. I guess with the little knowledge I have of both javascript and php you would need to make the video.php template file load a seperate header.php (header1.php), not sure how this can be done? Once again thank's for time and input. netnuta