0

I am using an ajax call to load mycontent.php into my modal dialog box. I am using AJAX, but the 3 javascript tags are not being included or being executed in the mycontent.php when it is in the modal window.

I have tried the eval statement but doesn't seem to work.

Here is my code:

main.html

 <a class="dialog" title="testvideo" href="mycontent.php">
 <img class="imageleft" width="200" height="150" src="assets/images/photos/deya/thumbnail.jpg"></a>

modal.js

    $(document).ready(function() {                

       $('a.dialog').click(function (event) {
          if ($("body").hasClass("res-full")){
             event.preventDefault();
             $this = $(this);

             var URL     = $(this).attr('href');
             var dialogbox = document.getElementById('dialog');
             var dialogOptions = {
                        height: 'auto',
                        width: 'auto',
                        modal: true,

              //         open: function(event, ui){

              //         },

                       close: function(event, ui){
                            $('#dialog').empty(); 
                       }
          };

       if(dialogbox==null) {
          $this.after("<div id=\"dialog\"></div>");
       }

       jQuery('#dialog').load(URL + " #content", function() {eval($('script').html())}).dialog(dialogOptions);


      }            
      });


    });

mycontent.php

   <div id="content">
       <div id="VidPlayerPlaceholder_7001_wrapper">
        <object id="VidPlayerPlaceholder_7001" width="100%" height="100%" type="application/x-shockwave-flash" data="video/player/player6.swf" bgcolor="#000000" name="VidPlayerPlaceholder_7001" tabindex="0">
        <div id="VidPlayerPlaceholder_7001_jwpsrv" style="position: absolute; top: 0px; z-index: 10;"></div>
   </div>
   <script type="text/javascript" src="http://www.mycompany.com/video/player/js/jwplayerv6.js"></script>
   <script type="text/javascript">jwplayer.key="jCz8k6TcT9i6M5vRXEI474+6dfNf9a7gHBbRfA==";</script>
   <script type="text/javascript">
          jwplayer('VidPlayerPlaceholder_4029').setup({ 
                    flashplayer: "http://www.company.com/video/player/player6.swf",
                    html5player: "http://www.company.com/video/player/js/jwplayer.html5.js",
                    playlist: [
            { image: "http://pciture.jpg", file: "rtmp://DEYA/video3.m4v"}
        ],
        width: '505',
        height: '430',
        stretch: 'uniform',
        autostart: 'false',
        repeat: 'false',

        logo: {
            file: 'http://ccopyright.png',
            link: 'http://www.mycompany.ca',
            hide: '',
            position: 'top-left'
        },
        rtmp: {
            bufferlength: '5'
        },
        primary: 'html5'
        });
        jwplayer('VidPlayerPlaceholder_4029').setVolume(50);
        if(0 > 0){
            jwplayer('VidPlayerPlaceholder_4029').stop();       
            jwplayer('VidPlayerPlaceholder_4029').seek(0);      
            if('false' == 'false'){
                jwplayer('VidPlayerPlaceholder_4029').pause();
            }
    }
</script> 

If anyone could help, that would be appreciated.

Moxie C
  • 442
  • 1
  • 15
  • 32

3 Answers3

1

You are calling load with URL + " #content". Adding a selector after the URL makes jQuery use just that part of the page and ignore the rest of it. So, your <script> tags are never loaded into the DOM nor are they available in the callback.

Try using $.get to get the entire page, then put each piece where it belongs.

$.get(url, function(html){
    // Parse HTML response
    var $data = $(html);

    // Add content to DOM
    $('#dialog').html($data.filter('#content').html());

    // Run scripts
    $data.filter('script').each(function(){
        // http://stackoverflow.com/a/12201713/206403
        var scriptTag = document.createElement('script');
        scriptTag.text = $(this).html();
        document.body.appendChild(scriptTag);
    });

   // Open dialog
   $('#dialog').dialog(dialogOptions);

}, 'html');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • I have tried the code and in firebug, it says there is a syntax error on , 'html'); – Moxie C Oct 08 '13 at 16:43
  • Works fine for me. Did you copy & paste it as is? – gen_Eric Oct 08 '13 at 17:09
  • Yes I did. Where did you put the code? I replaced the jQuery('#dialog').load(URL + " #content", function() {eval($('script').html())}).dialog(dialogOptions); with your code – Moxie C Oct 08 '13 at 17:12
  • It looks like the mycontent.php is not being loaded into the modal window. I do see the script tags in the main.html page when i view it in firebug. What I need now is the content to be loaded into the modal window plus the javascript in the modal window as well so it can run the video. – Moxie C Oct 08 '13 at 17:25
  • So, the AJAX call isn't firing? Maybe `$data.filter('#content')` should be `$data.find('#content')`, try that. – gen_Eric Oct 08 '13 at 17:30
  • i have used this $data.find('#content') and that loads the content into the dialog box. So we are one step closer. I see the script tags were copied from mycontent.php to the main.html. However, these script tags need to live within the modal window so it execute. How is this achievable? – Moxie C Oct 08 '13 at 17:48
  • @MoxieC: Try replacing `document.body.appendChild(scriptTag);` with `$('#dialog').append(scriptTag);`. – gen_Eric Oct 08 '13 at 17:49
  • i tried it, but that doesn't work either. I have tried $('#dialog').append($(this).html) and also $('#dialog').add($(this).html) – Moxie C Oct 08 '13 at 18:20
  • @MoxieC: Are the tags added the DOM with `$('#dialog').append(scriptTag);`? P.S. The 2 things you tried won't work. – gen_Eric Oct 08 '13 at 18:23
  • yes I did. It seems like it is appending it. However, it seems like it is stopping execution once it hits the last javascript tag. I have 5 of them. It doesn't print out the Hello. $('#dialog').append(scriptTag); }); alert("Hello"); $('#dialog').dialog(dialogOptions); – Moxie C Oct 08 '13 at 18:40
  • ReferenceError: jwplayer is not defined – Moxie C Oct 08 '13 at 19:03
  • Well, there's your problem. Where are you loading `jwplayer`? `jwplayer.key` won't work unless it's loaded. – gen_Eric Oct 08 '13 at 19:04
  • Right below the jwplayer.key has has the code for the jwplayer in the mycontent.php page. I just put the code in there – Moxie C Oct 08 '13 at 19:38
  • @MoxieC: Yes, but where do you include jwplayer? You can't use it unless you load its script file. – gen_Eric Oct 08 '13 at 19:44
  • Guess I missed it, i put the jwplayer.js script location above the jwplayer.key – Moxie C Oct 08 '13 at 19:57
  • You might want to add an empty div before appending the fragment content (that's how they do it in the load() if you look at the jquery source): $('
    ').append($.parseHTML(data)).find(fragment_div);$('#dialog').html(content);
    – am_ Oct 08 '13 at 20:07
  • Also you'll want to wrap your jwplayer('VidPlayerPlaceholder_4029').setup({ inside a custom event, and trigger that from your ajaxSuccess. Then it should work fine. – am_ Oct 08 '13 at 20:08
  • @am_ where do i put the $('
    ').append($.parseHTML(data)).find(fragment_div);$('#dialog').html(conten‌​t); code?
    – Moxie C Oct 08 '13 at 20:10
1

As i mentioned in the comments you could try doing it this way, I just built on Rocket Hazmat's answer - but replaced the fragment div with the implementation jquery uses for its load() with the #fragmentdiv param.

Now what you'll also want to do is call your jwplayer inside the success function (done()), paste in the JS for your jwplayer setup (the inline JS) you have from your php file into the done() function as commented below - and see if that works.

(function($) { //wrap to local scope
    $(document).ready(function() {
        var fragment_div = '#content';
        var $target_div = '#dialog';

        $.ajax({
            url: url,
            dataType: 'html' //IE can be buggy on the jquery intelligent guess
        }).done(function(data) {
            var content = $('<div>').append($.parseHTML(data)).find(fragment_div);
            $('#dialog').html(content);

            //try
            $target_div.html($data.filter('script'));
            //or
            $target_div.html($('<script>').append($.parseHTML(data)).find('script');

           //try moving your jwplayer setup code here for testing, you can use bind() and trigger()
           //to create a custom event if you want to keep the js as you have it now in mycontent.php
           jwplayer('VidPlayerPlaceholder_4029').setup({ /*..etc */ });

           // Open dialog
           $('#dialog').dialog(dialogOptions);
        });
    });
})(jQuery);

EDIT is there any extra content in your .php? If not you could just use .load() without the #fragment param. That will load the javascript aswell.

am_
  • 2,378
  • 1
  • 21
  • 28
  • Hi The problem is I have any script tags several empty tags and these should be as it seems to strip out the src. However a few of them have what is in between the script tags such as . Is there anyway of getting both the src and the ones that have content between the script tags. – Moxie C Oct 09 '13 at 15:45
  • I normally just use jquery's html() as it parses and evals the javascript inside. Try with my updated answer (target_div.html(this)) *EDIT* sorry that probably won't work - I'll do a quick test and update my answer soon. – am_ Oct 09 '13 at 21:14
  • Since you can't get the createElement('script') method to work, try one of the new ones I added in my answer. Don't have access to my dev enviro so I wasn't able to test this out. But you can give it a try. – am_ Oct 09 '13 at 21:27
0

Put the javascript on mycontent.php in a function, then you can call that function from the main.html when .load() completes. Read more about the onComplete functionality of .load() on http://api.jquery.com/load/

Edwin Otten
  • 122
  • 9