Turbojohan
Turbojohan

Reputation: 747

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...)

Upvotes: 0

Views: 1342

Answers (3)

Talha
Talha

Reputation: 19282

<body onload="RunScript();">

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

or

document.onload=function ...

Upvotes: 0

Emil Ivanov
Emil Ivanov

Reputation: 37673

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.

Upvotes: 0

Adil
Adil

Reputation: 148178

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

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

Upvotes: 2

Related Questions