简体   繁体   中英

Run javascript from external location after jQuery(document).ready()

An external javascript loads like this in a div in the page content:

<script type="text/javascript" src="http://example.com/example.js"></script>

The external script prints a sign up form for newsletter, like this:

document.write("<body>\n<form method=\"post\" action ETC....");

The problem is that the external server is slow and this third party script loads before jQuery(document).ready(), which deleays slideshows facebook plugins etc.

How can I make this script to render at it´s current position in the page content after the entire page has loaded?

(I have tried lot´s of suggested sollutions in different threads, but none worked for me...)

Use $(window).load it will be triggered after all the files/assets being downloaded.

$(window).load(function () {
  // run code
});

What you need to do is "inject" the script one the page has loaded:

$(function () {
     $('body').append('<script src="example.com/script.js"></script>');
});

This will execute on document ready but it's not a problem since the script will be loaded asynchronously.

<body onload="RunScript();">

function RunScript()
{
   document.write("<body>\n<form method=\"post\" action ETC....");
}

or

document.onload=function ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM