Loading scripts after page load?

后端 未结 7 1633
粉色の甜心
粉色の甜心 2020-11-27 15:16

I work with an advertising company, where we tag certain pages to track activity. A client of mine wants to fire off a javascript tag to track activity AFTER the page has fi

7条回答
  •  失恋的感觉
    2020-11-27 15:38

    The second approach is right to execute JavaScript code after the page has finished loading - but you don't actually execute JavaScript code there, you inserted plain HTML.
    The first thing works, but loads the JavaScript immediately and clears the page (so your tag will be there - but nothing else).
    (Plus: language="javascript" has been deprecated for years, use type="text/javascript" instead!)

    To get that working, you have to use the DOM manipulating methods included in JavaScript. Basically you'll need something like this:

    var scriptElement=document.createElement('script');
    scriptElement.type = 'text/javascript';
    scriptElement.src = filename;
    document.head.appendChild(scriptElement);
    

提交回复
热议问题