pass javascript in xmlHttp.responseText

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • trpost@gmail.com

    pass javascript in xmlHttp.responseText

    Is it possible to execute javascript as passed in xmlHttp.respons eText

    Here is what I am doing:

    search.js

    var xmlHttp
    xmlHttp=GetXmlH ttpObject()
    var url="search.php "
    xmlHttp.onready statechange=sta teChanged
    xmlHttp.open("G ET",url,true)
    xmlHttp.send(nu ll)

    function stateChanged()
    {

    if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" )
    {
    document.getEle mentById("searc h").innerHTML=x mlHttp.response Text;
    }

    }

    function GetXmlHttpObjec t()
    {

    var xmlHttp=null;

    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest( );
    }

    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }
    }
    return xmlHttp;

    search.php
    <script type="text/javascript">
    alert("hi");
    </script>
    <?php echo "hi"; ?>

    test.php
    <script src="search.js" ></script>
    <div id=livesearch></div>

    So what I see when I browse to test.php is 'hi' printed to the
    browser, but I would also expect to have the javascript alert executed

    If I visit search.php then I get 'hi' printed to the browser and the
    alert message

    So it seems that when I pass the response to the span the javascript
    alert is ignored
    (document.getEl ementById("sear ch").innerHTML= xmlHttp.respons eText;)

    Sp my question is how can I have javascript be passed and executed?

    Thanks!
  • slebetman

    #2
    Re: pass javascript in xmlHttp.respons eText

    On Nov 5, 3:50 am, trp...@gmail.co m wrote:
    Is it possible to execute javascript as passed in xmlHttp.respons eText
    <snip>
    Sp my question is how can I have javascript be passed and executed?
    >
    // One way is to manually find all new script tags and eval them:

    var searchElement = document.getEle mentById("searc h");
    searchElement.i nnerHTML=xmlHtt p.responseText;
    var scripts = searchElement.g etElementsByTag Name('script');
    for (var i=0;i<scripts.l ength;i++) {
    eval(scripts[i].innerHTML);
    }

    Comment

    • David Mark

      #3
      Re: pass javascript in xmlHttp.respons eText

      On Nov 4, 2:50 pm, trp...@gmail.co m wrote:
      Is it possible to execute javascript as passed in xmlHttp.respons eText
      Yes. Even easier with responseXML.
      >
      Here is what I am doing:
      >
      search.js
      >
      var xmlHttp
      xmlHttp=GetXmlH ttpObject()
      Is that a constructor?
      var url="search.php "
      xmlHttp.onready statechange=sta teChanged
      xmlHttp.open("G ET",url,true)
      xmlHttp.send(nu ll)
      Is your semicolon key broken?
      >
      function stateChanged()
      {
      >
      if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" )
      Lose that second test.
      {
              document.getEle mentById("searc h").innerHTML=x mlHttp.response Text;
      Here is the problem. You could feature test the innerHTML property to
      see if it will evaluate inline scripts, but IIRC, using standard DOM
      methods (e.g. appendChild) avoids this problem altogether.

      Alternatively, when you send your Ajax request, include a header to
      indicate to the test.php script that it should send the document as
      XHTML (with text/xml as the MIME type, IIRC.) Then you can import the
      nodes as needed (even in IE.)
      >
      }
      }
      >
      function GetXmlHttpObjec t()
      Not a constructor.
      {
      >
      var xmlHttp=null;
      >
      try
      {
              // Firefox, Opera 8.0+, Safari
      And IE7, but slightly crippled.
              xmlHttp=new XMLHttpRequest( );
      >
      }
      >
      catch (e)
      Using try-catch in lieu of feature detection is not a good strategy.
      {
              // Internet Explorer
      And anything else that failed above.
              try
              {
                      xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
              }
              catch (e)
              {
                      xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
              }}
      >
      return xmlHttp;
      >
      search.php
      <script type="text/javascript">
           alert("hi");
      </script>
      <?php echo "hi"; ?>
      >
      test.php
      <script src="search.js" ></script>
      <div id=livesearch></div>
      >
      So what I see when I browse to test.php is 'hi' printed to the
      browser, but I would also expect to have the javascript alert executed
      Setting the innerHTML property does not execute the inline scripts in
      IE (as well as other agents, IIRC.)
      >
      If I visit search.php then I get 'hi' printed to the browser and the
      alert message
      As expected.
      >
      So it seems that when I pass the response to the span the javascript
      When you set the innerHTML property of the div.
      alert is ignored
      Right.

      [snip]

      Comment

      Working...