0

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

4
  • If you won't explain and demonstrate what exactly you've tried and how it failed, you're likely to get the same answers. Commented Feb 7, 2013 at 10:11
  • just put the document write inside the ready? Commented Feb 7, 2013 at 10:13
  • 2
    Since the external script is creating the <body> element, how can yo display anything before it's loaded? Commented Feb 7, 2013 at 10:15
  • 2
    A third-party script that prints the <body> tags? Put it into an iframe. Commented Feb 7, 2013 at 10:32

3 Answers 3

2

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

$(window).load(function () {
  // run code
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but how do I run a script from an external location which outputs with document.write?
0

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.

2 Comments

a) no reason to wait with async loading for ready b) this does not work with the script above since it uses document.write
This did not work with the script! This won´t make the output render where in the code the script is called, will it?
0
<body onload="RunScript();">

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

or

document.onload=function ...

3 Comments

There is no function to call. As you see this script is renderd where it is called with document.write.
@Turbojohan in your onload function of RunScript() you can render html as in my updated answer
I see, but how do I call the script from an external location?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.