1

The goal is to load script-Tags per ajax, execute the scripts and show the content of the script-tag (a iframe with a video).

The scenario: The scenario is a video page. If i click on "video-text" the id in the dom will be determined. After that an ajax-php-request will be executed with the determined id. This request will responese with a script tag. And this is the problem: How can i get the iframe? Have anyone a tip for me?

ajax-call:

$.ajax({
 type: "GET",
 url: "/get-embed-by-id/"+$(this).find('.views-field-nid .field-content').html(),
 success: postToPageTwo,
 error : postToPageTwo,
 datataype: "html",
});

This call returns a script like this one

<script type="text/javascript" src="http://exampleurl.de/video/7"></script>

The content of this script is for example:

(function(){ 
 document.write('<iframe id="video_11" class="mediacube_video" 
 src="http://exampleurl.de/video/11.html?" scrolling="no" 
 align="center" frameborder="0" framespacing="0" width="725" height="409">
 </iframe>');
})()

EDIT the code to use with $.getScript:

$.getScript("http://exampleurl.de/video/5", scriptResponse);

and the code of scriptResponse:

function scriptResponse(data) {
 console.log("data:");
 console.log(data);
}

but the data is always undefined. or did i anything wrong?

asked Apr 18, 2012 at 8:42

1 Answer 1

2

jQuery has .getScript() to load and execute scripts for you. no need to call an ajax to return an html to load another script (that's a lot of round trips) just to call a script.

$.getScript('http://exampleurl.de/video/7');

if the initial request really matters (maybe some parsing on the back-end to determine the link of the script), you can have the initial response return just the url of the script. then use that returned string for the .getScript()

$.get('initial_url',function(returned_url){
 $.getScript(returned_url);
});

but still, too many round trips.

answered Apr 18, 2012 at 8:44
Sign up to request clarification or add additional context in comments.

3 Comments

try console.log() inside the function of the script you are fetching. if it does appear, then the script has executed.
ok i got a few other problems, but this was the correct answer of you. So i mark this as answered. Thanks!
From the getScript docs: "Success Callback: The callback is fired once the script has been loaded but not necessarily executed."

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.