Jason Axelrod
Jason Axelrod

Reputation: 7805

Loading Javascript through an AJAX load through jQuery?

I have an javascript that I place into a page using the code below. What the code does is place an object/embed code into a webpage. Simple javascript loader to a NicoVideo movie

<script type="text/javascript" src="http://ext.nicovideo.jp/thumb_watch/sm13154955?w=640&h=395"></script>

This works great in a webpage. But what if I want to load this javascript into a page using AJAX? This no longer works for the obvious reasons, you would need to eval the script in order to get it to run. However, I have no idea how to do this. I am using jQuery on my page; so keep that in mind. I have tried the following code, but it doesn't seem to work through AJAX, or even in a normal page load environment.

<script>$.getScript("http://ext.nicovideo.jp/thumb_watch/sm13154955?w=640&h=395");</script>

Any ideas on how I would get this to work?

Upvotes: 1

Views: 1166

Answers (2)

darioo
darioo

Reputation: 47183

After reading official getScript reference, it seems you have to do something with that JS file you got a hold of, using something like this:

$.getScript("http://ext.nicovideo.jp/thumb_watch/sm13154955?w=640&h=395", function () {
   // use functions from loaded file
});

Upvotes: 1

Dave G
Dave G

Reputation: 9767

I think it works but its attempting to inline the write which I don't know if that would work in this case.

You would need to see if there was a way to essentially execute the '.getHTML' method and take that result and update an existing element on the page.

The issue though is that the anonymous function that is generated and executed inline might not work properly.

Upvotes: 2

Related Questions