Executing [removed] injected by innerHTML after AJAX call

前端 未结 11 1597
误落风尘
误落风尘 2020-11-22 05:02

There\'s a div called \"Content\":

It should be filled with data from a PHP file, by AJAX, including

相关标签:
11条回答
  • 2020-11-22 05:28

    Here is a function you can use to parse AJAX responses, especially if you use minifiedjs and want it to execute the returned Javascript or just want to parse the scripts without adding them to the DOM, it handles exception errors as well. I used this code in php4sack library and it is useful outside of the library.

    function parseScript(_source) {
        var source = _source;
        var scripts = new Array();
    
        // Strip out tags
        while(source.toLowerCase().indexOf("<script") > -1 || source.toLowerCase().indexOf("</script") > -1) {
            var s = source.toLowerCase().indexOf("<script");
            var s_e = source.indexOf(">", s);
            var e = source.toLowerCase().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 {
              if (scripts[i] != '')
              {         
                try  {          //IE
                      execScript(scripts[i]);   
          }
          catch(ex)           //Firefox
          {
            window.eval(scripts[i]);
          }   
    
                }  
            }
            catch(e) {
                // do what you want here when a script fails
             // window.alert('Script failed to run - '+scripts[i]);
              if (e instanceof SyntaxError) console.log (e.message+' - '+scripts[i]);
                        }
        }
    // Return the cleaned source
        return source;
     }
    
    0 讨论(0)
  • 2020-11-22 05:32

    JavaScript inserted as DOM text will not execute. However, you can use the dynamic script pattern to accomplish your goal. The basic idea is to move the script that you want to execute into an external file and create a script tag when you get your Ajax response. You then set the src attribute of your script tag and voila, it loads and executes the external script.

    This other StackOverflow post may also be helpful to you: Can scripts be inserted with innerHTML?.

    0 讨论(0)
  • 2020-11-22 05:32

    I used this code, it is working fine

    var arr = MyDiv.getElementsByTagName('script')
    for (var n = 0; n < arr.length; n++)
        eval(arr[n].innerHTML)//run script inside div
    
    0 讨论(0)
  • 2020-11-22 05:37

    I had a similiar post here, addEventListener load on ajax load WITHOUT jquery

    How I solved it was to insert calls to functions within my stateChange function. The page I had setup was 3 buttons that would load 3 different pages into the contentArea. Because I had to know which button was being pressed to load page 1, 2 or 3, I could easily use if/else statements to determine which page is being loaded and then which function to run. What I was trying to do was register different button listeners that would only work when the specific page was loaded because of element IDs..

    so...

    if (page1 is being loaded, pageload = 1) run function registerListeners1

    then the same for page 2 or 3.

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

    If you are injecting something that needs the script tag, you may get an uncaught syntax error and say illegal token. To avoid this, be sure to escape the forward slashes in your closing script tag(s). ie;

    var output += '<\/script>';
    

    Same goes for any closing tags, such as a form tag.

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