Executing javascript script after ajax-loaded a page - doesn't work

后端 未结 4 1033
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 13:37

I\'m trying to get a page with AJAX, but when I get that page and it includes Javascript code - it doesn\'t execute it.

Why?

Simple code in my ajax page:

相关标签:
4条回答
  • 2020-12-11 14:05

    When you update your page by doing something like setting a container's innerHTML to some updated content, the browser simply will not run the scripts in it. You can locate the <script> tags, get their innerHTML (IE may prefer innerTEXT), and then eval() the scripts yourself (which is pretty much what jQuery does, though it finds the scripts with a regex before updating the DOM).

    0 讨论(0)
  • 2020-12-11 14:11

    In case some other people stumble upon this old thread, there is one issue with the accepted answer by Deukalion, there is one issue that may have been overlooked: as written, the script only looks for the first script tag. If multiple script tags exist, all others are overlooked.

    A few minor tweaks would resolve the issue. Change one line from:

        var script = content.match("<script[^>]*>[^<]*</script>");
    

    To:

        var script = content.match(/<script[^>]*>[^<]*<\/script>/g);
    

    And another from:

        script = script.toString().replace('<script type="text/javascript">', '');
    

    To:

        script = script.join("").replace(/<script type="text\/javascript">/g, '');
    

    Now it will gather all the <script> code and execute them in the order found on the page. Otherwise it was an excellent solution.

    0 讨论(0)
  • 2020-12-11 14:25

    After the AJAX request, you can make an "on success" function which can take the returned html and do something with it. Then something will be executed.

    If there was a code example, then I could provide a code solution to the situation. But using just standard xmlhttprequest, the following could be done:

    xhr = new XMLHttpRequest();
    xhr.open("GET","ajax_info.txt",true);
    xhr.onreadystatechange=function()
    {
    if (xhr.readyState==4 && xhr.status==200)
      {
      document.getElementById("myDiv").innerHTML = xhr.responseText;
      }
    }
    xhr.send();
    ​
    
    0 讨论(0)
  • 2020-12-11 14:28

    Use this function:

    function parseScript(_source) {
        var source = _source;
        var scripts = new Array();
    
        // Strip out tags
        while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
            var s = source.indexOf("<script");
            var s_e = source.indexOf(">", s);
            var e = source.indexOf("</script", s);
            var e_e = source.indexOf(">", e);
    
            // Add to scripts array
            scripts.push(source.substring(s_e+1, e));
            // Strip from source
            source = source.substring(0, s) + source.substring(e_e+1);
        }
    
        // Loop through every script collected and eval it
        for(var i=0; i<scripts.length; i++) {
            try {
                eval(scripts[i]);
            }
            catch(ex) {
                // do what you want here when a script fails
            }
        }
    
        // Return the cleaned source
        return source;
    }
    

    then do parseScript(xmlhttp.responseText); when you're replacing/adding content.

    0 讨论(0)
提交回复
热议问题