JavaScript returned by AJAX is not working. How to work with JavaScript?

前端 未结 6 1199
天涯浪人
天涯浪人 2020-12-12 06:52

this is my page Test1.asp






        
相关标签:
6条回答
  • 2020-12-12 07:14

    Your methodology is slightly amiss. Typically, AJAX is used to send and receive data in non-HTML formats, such as XML, JSON, or sometimes even CSV (however, HTML is sometimes returned to the client, but usually as pieces of a page, not entire pages as in your example).

    Logic is rarely transmitted and is usually maintained on the respective sides of the transmission. In other words, the client/request side has all of its own logic and already knows what to do with the data returned from the server/response side (which also doesn't accept or require any logic generated from the client side). Further, the use of eval, which is usually necessary to consistently execute the logic found in the response, is generally frowned upon and considered a bad practice, thus the saying, "eval is evil."

    In some cases, it may be necessary, advantageous or just plain easier to receive logic as part of the response from the server. In these situations however, it is still considered a best practice to separate your data from your logic.

    All that to nicely say that you're doing it wrong. I encourage you to read up on how AJAX works and how best to use it: w3schools walk-through, Mozilla MDC intro, AJAX and XML processing, updating a page (similar to what I think you're trying to do), and finally, AJAX API docs for jQuery, Prototype and Dojo.

    0 讨论(0)
  • 2020-12-12 07:25

    In HTML inserted by Javascript does not execute automatically (at least in IE for sure). The only solution to this is to gather each of the script blocks in the loaded HTML and evaluate them each.

    EDIT

    I am using YUI here... the Dom class can collect all script tags from within the given block.

    var domElement = document.getElementById("Alex");
    var scriptBlocks = YAHOO.util.Dom.getElementsBy(function() {return true;},'script',domElement);
    for (var i = 0 ; i < scriptBlocks.length ; ++i){
        eval(scriptBlocks[i].innerHTML);
    }
    

    Simple as that. Also becareful about Internet Explorer... if you load in HTML using ajax, and it comes back with the script block as one of the first elements, it will, for some odd reason, ignore the script block and not include it in the response. To fix it, put a div above the script block with text in it with a style attribute of display:none;

    If this is the HTML returned to IE, it will not include the script block in the response

    <div>
       <script type="text/javascript">
         /* Some javascript */
       </script>
    </div>
    

    This will fix the issue

    <div style="display:none;">some text</div>
    <div>
       <script type="text/javascript">
         /* Some javascript */
       </script>
    </div>
    

    Very weird, but thats how IE rolls.

    0 讨论(0)
  • 2020-12-12 07:29

    Adding a <script> element into a document via innerHTML doesn't(*) execute its contents as a script.

    You're also trying to insert the entire HTML document, including <html>, <head> and <body> inside a <div>, which is quite invalid.

    If you need to return both HTML and some script to execute, better to return a JSON object, eg.:

    {
        "html": "<div id="Mathew"></div>",
        "js": "document.getElementById(\"Mathew\").innerHTML='ajax is working';"
    }
    

    then parse the JSON object, set the innerHTML to obj.html and eval the js. (Though it's generally questionable to be returning and executing arbitrary, scripts, there can sometimes be a use for it.)

    (*: Well, doesn't generally. Exactly when a <script> element's contents get executed is browser-dependent. For example Firefox executes script when you append/insert a DOM HTMLScriptElement or ancestor into an element that is part of the document, whereas IE executes it when you insert the element into any parent for the first time, whether inside the document or not. In general, avoid inserting JavaScript-in-HTML content into HTML.)

    0 讨论(0)
  • 2020-12-12 07:33

    By default JavaScript contained with AJAX responses is not executed.

    There is no point in building an Ajax handler from scratch when this problem has already be solved in various libraries just as jQuery and Prototype.

    0 讨论(0)
  • 2020-12-12 07:33

    Use an absolute URI instead of a relative URL.

    0 讨论(0)
  • 2020-12-12 07:34

    Prototype has a nice way of handling this. See their stripScripts, extractStrips, and evalScripts methods on the String object. If you just strip the scripts, put your text into a div and then evalScripts, that'll work across all brwosers so scripts get executed exactly once.

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