jQuery .load() call doesn't execute JavaScript in loaded HTML file

后端 未结 13 1709
难免孤独
难免孤独 2020-11-22 08:28

This seems to be a problem related to Safari only. I\'ve tried 4 on Mac and 3 on Windows and am still having no luck.

I\'m trying to load an external HTML file and

相关标签:
13条回答
  • 2020-11-22 08:36

    This doesn't seem to work if you're loading the HTML field into a dynamically created element.

    $('body').append('<div id="loader"></div>');
    $('#loader').load('htmlwithscript.htm');
    

    I look at firebug DOM and there is no script node at all, only the HTML and my CSS node.

    Anyone have come across this?

    0 讨论(0)
  • 2020-11-22 08:38

    You are loading an entire HTML page into your div, including the html, head and body tags. What happens if you do the load and just have the opening script, closing script, and JavaScript code in the HTML that you load?

    Here is the driver page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"> 
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>jQuery Load of Script</title>
            <script type="text/javascript" src="http://www.google.com/jsapi"></script>
            <script type="text/javascript">
                google.load("jquery", "1.3.2");
            </script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $("#myButton").click(function() {
                        $("#myDiv").load("trackingCode.html");
                    });
                 });
             </script>
        </head>
        <body>
            <button id="myButton">Click Me</button>
            <div id="myDiv"></div>
        </body>
    </html>
    

    Here is the contents of trackingCode.html:

    <script type="text/javascript">
        alert("Outside the jQuery ready");
        $(function() {
            alert("Inside the jQuery ready");
        });
     </script>
    

    This works for me in Safari 4.

    Update: Added DOCTYPE and html namespace to match the code on my test environment. Tested with Firefox 3.6.13 and example code works.

    0 讨论(0)
  • 2020-11-22 08:42

    I was able to fix this issue by changing $(document).ready() to window.onLoad().

    0 讨论(0)
  • 2020-11-22 08:46

    You've almost got it. Tell jquery you want to load only the script:

    $("#myBtn").click(function() {
        $("#myDiv").load("trackingCode.html script");
    });
    
    0 讨论(0)
  • 2020-11-22 08:50

    I wrote the following jquery plugin for html loading function: http://webtech-training.blogspot.in/2010/10/dyamic-html-loader.html

    0 讨论(0)
  • 2020-11-22 08:52

    Test with this in trackingCode.html:

    <script type="text/javascript">
    
        $(function() {
    
           show_alert();
    
           function show_alert() {
               alert("Inside the jQuery ready");
           }
    
        });
     </script>
    
    0 讨论(0)
提交回复
热议问题