jquery - Javascript adding <script> tag after page loads -


got little problem here. basically, i'm trying add script tag after page loads.

this doing:

index.php:

<html> <head>    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>    <script>    function getad()    {         $.post('assets/getad.php', "ad", function(response) {             response = response.replace('document.write','document.getelementbyid("ad").innerhtml = ');             eval(response);             console.log(response);         });    }    getad();    </script>     <div id="ad"></div> </body> </html> 

getad.php:

<?php     echo file_get_contents("http://ads1.qadabra.com/t?id=a823aca3-9e3c-4ddd-a0cc-14b497cad85b&size=300x250"); ?> 

you can find demo here: http://dev.cj.gy/game/

as can see, #ad div filled correct script tag, doesnt run, if edit page include script tag right @ page load, run.

yes, <script> tags cause execution when parsed part of main document; don't execute being written innerhtml.

you can create executing script element outside of initial parse using dom method of calling createelement('script'), setting src/content , adding document. jquery's getscript does.

however wouldn't because next script, ads1.qadabra.com document.writeing page, calls document.write.

you work way around both of these calls @ client side (ie without getad.php), assigning own custom function document.write that, instead of writing loading page, attempts extract source of script tag passed it, , load in dom-created script element.

but in general these scripts designed work synchronously @ document load time; try force them run in way weren't intended fragile , stop working when ad network change anything.

if want load third-party ad without pausing loading of parent document, suggest putting in iframe.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -