“getElementById not a function” when trying to parse an AJAX response?

后端 未结 3 652
死守一世寂寞
死守一世寂寞 2020-11-29 10:36

I\'m running GM_xmlhttpRequest (in a Greasemonkey script) and storing the responseText into a newly created HTML element:

var respo         


        
3条回答
  •  无人及你
    2020-11-29 11:42

    Use DOMParser() to convert responseText into a searchable DOM tree.
    Also, your attempts to search/use anything derived from responseText, must occur inside the onload function.

    Use code like this:

    GM_xmlhttpRequest ( {
        ...
        onload:     parseAJAX_ResponseHTML,
        ...
    } );
    
    function parseAJAX_ResponseHTML (respObject) {
        var parser      = new DOMParser ();
        var responseDoc = parser.parseFromString (respObject.responseText, "text/html");
    
        console.log (responseDoc.getElementsByTagName('div'));
        console.log (responseDoc.getElementById('result_0'));
    }
    


    Of course, also verify that a node with id result_0 is actually in the returned HTML. (Using Firebug, Wireshark, etc.)

提交回复
热议问题