views:

26

answers:

3

I just started with jQuery and so far I have a really good impression of the framework. But now I have a problem that I can't figure out on myself. I want to embed an external javascript file that loads a videoplayer and displays it. If I include the <script>...</script> directly in HTML it loads just fine. But when I try to load it with jQuery it has very different behaviors in several browsers but it doesn't work in a single one. Either the player is not loaded at all or it doesn't update the selected div but reloads the player on a blank page. This is the source:

<html>
<head>
<style type="text/css"> 
  div#test {
  width: 100%;
  height: 500px;
  margin: 10px auto;
  padding: 5px;
  border: 1px solid #777;
  background-color: #fbca93;
  text-align: center;
 }
</style> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script>
  $(document).ready(function(){

  var video = "<script type=\'text\/javascript\' src=\'http:\/\/www.physicalfitnet.com\/video_syndication\/embed\/jssingle.aspx?vid=651&pub=9DF8233261D101B86368400385B8FF2E\'><\/script>";
  $("div#test").html(video);     

  });
</script>
</head>

<body>
<div id="test"> Hi</div> 
</body>
</html>

I uploaded everything to http://kernast.de/test ... any help is appreciated. THX

+1  A: 

I couldn't get at the script (got an ASP NullReferenceException trying to download the script), but is it using document.write to write to the page? Because if so, those scripts must be included during the parsing stage, they can't be added afterward. This is because they actually emit HTML to the document for the parser to work with. Calling document.write after the document has been parsed doesn't do anything.

T.J. Crowder
I really don't know if they use document.write in their script but your explanation makes total sense then. I tried to embed it in many ways by using jQuery and it never worked. So I guess as I can't use jQuery for that, right ?!
Dennis
@Dennis: You should be able to tell quite easily whether they use `document.write` in their script: I don't know why I was getting a NullReferenceException from the server it's on, but if it works when you include the script tag in the page itself, do that and then use Firebug or Chrome's DevTools or whatever to look at the script that came back and see if it uses `document.write` anywhere. If it does, then you can't include it by adding it to the DOM later; you'd have to include it in a dynamically-created iframe or some such.
T.J. Crowder
A: 

It seems like its a script that does some document.write. The only logical way to include this script is to embed the script using the <script> tag inside whatever div.

Do let me know if the video script accepts any useful parameters such as ID of the DIV in which to embed the player, or a callback function. You might be able to use jQuery.getScript() function in this case.

Salman A
A: 

@T.J. Crowder: You were right... they use document.write in their script. I will then look for a solution concerning the dynamically created iframe.

@Salman A: No parameters are accepted.

Thx for your advice!

Dennis