1

When using responseHTML with XHR, Firefox executes the javascripts within the loaded responseHTML, chromium does not. if i add a script that will parse and execute the scripts, it will work on chromium but they result being executed twice in Firefox. Any idea how to know if browser will execute loaded scripts or not other than through agent sniffing? PS: I'm using a JS framework that IS NOT jQuery or Prototype or anything

1
  • well, i've found a way to do it, but its kinda ugly, so i'm leaving this question open Commented Jun 28, 2010 at 1:20

3 Answers 3

1

This is the n'th time I've answered this question now :)

// response is the data returned from the server
var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";

var reScript = /\<script.*?>(.*)<\/script>/mg;
response = response.replace(reScript, function(m,m1) {
    eval(m1); //will run alert("foo");
    return "";
});
alert(response); // will alert "htmlhtml"
Sign up to request clarification or add additional context in comments.

2 Comments

thx sean but that was not my question. If i do what you suggest, script will be executed once in chromium but twice in firefox who natively execute scripts. I've found a way by prepending a javascript to innerHTML that will tell the onload script to execute or not.
That's not true. The replace removes the script before inserting it into the DOM, so it only executes in the eval. Did you actually try this?
0

Don't sniff. Test. I don't remember the mechanics of exactly when Mozilla fires the scripts, but it should be possible to manufacture an internal test that detects if scripts are fired and then to patch accordingly.

1 Comment

i've been googling for the last couple of hours and no response. I'm kinda dazzled, seems like a pretty important x-browser issue though
0

Don't use responseHTML. Use responseText instead and use the following function. The onSuccess function is important to you. Use it in your success handler of ajax request.

 function showHtmlInElement(targetId, htmlUrl)  {
    var target = document.getElementById(targetId);
    var Util = jaf.core.Util;
    Util.ajax({
       url: htmlUrl,
       dataType: "text/html",
       // ---------------------------------------------------------------
       onSuccess: function(xhr) {
          var responseText = xhr.responseText;
          target.innerHTML = responseText;
          // collect all the script tag content...
          var scriptText = "";
          var arrScripts = target.getElementsByTagName("script");
          for(var i = 0, len = arrScripts.length; i < len; i++) {
             var se = arrScripts[i];
             scriptText += se.innerHTML;
          }
          if((scriptText = Util.trim(scriptText)).length != 0)  {
             eval.call(window, scriptText);
          }
       },
       // ---------------------------------------------------------------
       onError: function(xhr, code, message) {
          var responseText = xhr.responseText;
          var content = "<p class='error'>" + responseText + ": " 
                       + message + "(" + code + ")</p>";
          target.innerHTML = content;
       }
    });
 }

I've tested this opera, firefox, chromium, safari. Haven't tested in IE6 though.

1 Comment

Thanks man, i'll investigate it... also on IE6 to let you know :)

Your Answer

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.