javascriptjqueryperformanceoptimizationopenx

What to do when function need to be called before script load


I'm calling this function:

<script type='text/javascript'><!--// <![CDATA[
    OA_show(18);
// ]]> --></script>

at many sites on a page but the script is loaded at bottom as follow:

<script type='text/javascript' src='//openx.elclarinweb.com/www/delivery/spcjs.php?id=2&#038;target=_blank&#038;ver=20141010'></script>

which causes this error:

Uncaught ReferenceError: OA_show is not defined

Note

I can't call the function after script loads because it needs to be called on specific position where Ads will be show, take a look here to see what I'm talking about

Is there any way to get ride of the error? Should I load the script at top even if it's obtrusive and prevent the page to load before it loads completely? Any advice?


Solution

  • put in onload:

    window.onload = function(){
       OA_show(18);
    };
    

    This waits for all the resources to be loaded and then executes the function you are calling.