13

I am dynamically adding a script of a github gist. If I added it to the html before the page loads, the script will execute. But If I try to add it to the page after it loads it adds the script to the page but doesn't execute it.

Here is how I am adding it to the page

Html

<div id="results"></div>

Javascript

$('#results').click(function() {
 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = "https://gist.github.com/1265374.js?file=GetGists.js";
 document.getElementById('results').appendChild(script);
 console.log('script added');
});

Here is the live example

http://jsfiddle.net/guanome/JqB3g/3/

asked Oct 13, 2011 at 22:25

6 Answers 6

9
+50

So, the best option I can see is to override (even if just temporarily while the script loads) document.write. It is sort of a work around, but since you don't control the code coming in, I think it may be the best you've got. You also shouldn't be using document.write anytime after the page loads, but just in case, I'd maybe comb through your code.

Here's a fiddle with a working solution. If you wanted to revert document.write after the script loaded, you can always check to see if script.complete is true, otherwise, listen for an onload event, which should allow you to change document.write back. I'm too lazy to code it into the fiddle at the moment, but this would be the code generally:

script.src = "...";
document.getElementById("results").appendChild(script);
if(script.complete) document.write = document._write;
else script.load(function() { 
 // Goes to the end of the run queue so that the script 
 // is guaranteed to run before this code
 setTimeout(function(){document.write = document._write;},0);
});
answered Oct 25, 2011 at 3:35
Sign up to request clarification or add additional context in comments.

1 Comment

This method seems to work the best without any outside references.
3

It's executing, but it won't work since it uses document.write, which can only be used synchronously. Include async document.write, and modify your script as such:

$('#results').click(function() {
 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = "https://gist.github.com/1265374.js?file=GetGists.js";
 document.write.to = {filename: script.src};
 document.getElementById('results').appendChild(script);
 console.log('script added');
});
answered Oct 24, 2011 at 21:53

3 Comments

... or don't use document.write in the first place if you can get away with it
@missingno I would, but I don't control what github is supplying in their embed code.
@Eli Grey, I added your code and included async document.write to a fiddle, and it is adding the script to the page, but it's not executing the script that has been added to the page.
2

I was able to accomplish this after hours of trying, including all other answers here. My script tag looks like this:

<script src="https://somedomain.com/some.js"></script>

so no eval() was possible.

I've used jQuery.getScript() in the end, which fetches the js file and executes it:

var $scriptTag = $('#myElement script');
var scriptSrc = $scriptTag.attr('src');
$.getScript(scriptSrc);
Mig82
5,6264 gold badges46 silver badges71 bronze badges
answered Nov 28, 2017 at 14:25

Comments

1

So, as an alternative, I actually just stumbled upon a library which seems like it would handle this for you. Check out Injector.js, and it should work for you.

answered Oct 25, 2011 at 18:08

Comments

0

your script is executing , you just can't use document.write from it. Use an alert to test it and avoid using document.write. The statements of your js file with document.write will not be executed and the rest of the function will be executed.

answered Jun 21, 2013 at 16:07

Comments

-4

Try $(this).append(script) or $(this).html(script)

answered Oct 13, 2011 at 22:35

1 Comment

I'm not sure how that is different from what I am doing. this is just referring to the #results div. I am already doing this with basic javascript.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.