1

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?

Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78

2 Answers2

2

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.

Dave G
  • 9,639
  • 36
  • 41
  • Ok back to basics. When you include this script tag as you indicated above that is working, you have that inside a 'dom' element like a '

    ' or some other tag. The way that javascript is crafted, it will be executed inline at page load. As such, when you load via AJAX, it may not necessarily execute at the time it is requested, rather after the page is rendered. So essentially, the elements for the video display will be written into the document say after the 'html' close element. Essentially, you want to replace the contents of an element with this player using jQuery to do the repl.

    – Dave G Jan 07 '11 at 15:15
  • Yes, the javascript is encapsulated in a DIV called "embed_player". This works on a normal page, but it wont work in AJAX... how do I get this working in AJAX? – Jason Axelrod Jan 07 '11 at 15:23
  • Jason, that's what I was saying. When used inline, the document.write calls used by the anonymous function declared at the end of the javascript will place the content for the player outside the HTML as it is technically done asynchronously. If I might ask, why are you trying to do this via ajax? is it a replacement after the page loads? – Dave G Jan 07 '11 at 16:03
  • Its to prevent loading too many Object/Embed codes on a forum. I see too often where there are 50 youtube videos on a page and it chugs along and kills browsers... example here: http://www.8wayrun.com/threads/media-this-is-a-test.6520/ (click the play button) – Jason Axelrod Jan 07 '11 at 16:07
  • Ok well unless there is a mechanism provided with the nico player to alter the returned JS so that it doesn't execute that anonymous function you're kind of stuck. If there were a way to pull that JS in via AJAX, then use the getHTML functionality to generate the new HTML to inject in the page you could use jQuery to replace the content of the element in question at that point and be able to play. – Dave G Jan 07 '11 at 21:41
1

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
});
Aistina
  • 12,435
  • 13
  • 69
  • 89
darioo
  • 46,442
  • 10
  • 75
  • 103