开发者

jQuery on responseXML in a Firefox ext

I'm trying to allow jQuery 1.4.2 work on a responseXML variable, but it doesn't... I can only let it work with the live page displayed. Here's my code:

var url = myext.baseuri + "/Common/page.asp";
   var httpRequest = myext.createHttpRequest();

   httpRequest.open('GET', url, false);
   httpRequest.send();
   if (httpRequest.status == 200) {

      $h = function(selector,context){ return new jQuery.fn.init(selector,context||httpRequest.responseXML); };

      Firebug.Console.log($h().find("select#id").html());
      Firebug.Console.log($h(httpRequest.responseXML));
      Fir开发者_运维技巧ebug.Console.log($h(httpRequest.responseXML).html());

   } else {
      Firebug.Console.warn("status "+ httpRequest.status);
   }

I see the following results in Firebug console:

  • Null
  • jQuery(Document )
  • Null

I don't understand how I should utilize jQuery in this case... why the first row returns Null? I really need some help...

Thanks!


You shouldn't use html() on XML documents, as it is specified in jQuery documentation : http://docs.jquery.com/Html.

If you want to get the text contained in the "select#id" element, you should try

Firebug.Console.log($h("select#id").text());

as the text() function can be used on XML documents (cf jQuery documentation).

If you want to get another element of your XML response, you can get it like this :

Firebug.Console.log($h("tagName1 tagName2 tagName3"));

I haven't tried to get elements by Id like you tried with "#id", but I think that it may be not supported by jQuery for XML document as it calls "document.getElementById()". You may try instead :

Firebug.Console.log($h("select[id='id']"));

You will not have the same performances with this method, but it may work !


Here is an example of what i am working on :

var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                             .createInstance(Components.interfaces.nsIXMLHttpRequest);
req.onload = function(){
    var xml = req.responseXML;
    $xml = function(selector){ return new jQuery.fn.init(selector,xml); };
    Firebug.Console.log($xml("schedule[order='1'] item"));
};
req.open("GET", url, true);
req.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
req.send(null);

And the Firebug Console :[item, item, item, item]

Here is an example of the XML I am working on :

<?xml version="1.0" encoding="UTF-8"?>
<ratp>
    <schedules>
        <schedule order="1">
            <title><![CDATA[Prochains passages en temps réel]]></title>
            <liste>
                <item order="1">
                    <texte1><![CDATA[Voltaire-Villiers]]></texte1>
                    <texte2><![CDATA[11 mn]]></texte2>
                    <texte3><![CDATA[]]>
                    </texte3>
                </item>
                <item order="2">
                    <texte1><![CDATA[Voltaire-Villiers]]></texte1>
                    <texte2><![CDATA[30 mn]]></texte2>
                    <texte3><![CDATA[]]>
                    </texte3>
                </item>
                <item order="3">
                    <texte1><![CDATA[Place Jean Poulmarch]]></texte1>
                    <texte2><![CDATA[4 mn]]></texte2>
                    <texte3><![CDATA[]]>
                    </texte3>
                </item>
                <item order="4">
                    <texte1><![CDATA[Place Jean Poulmarch]]></texte1>
                    <texte2><![CDATA[24 mn]]></texte2>
                    <texte3><![CDATA[]]>
                    </texte3>
                </item>
            </liste>
        </schedule>
    </schedules>
</ratp>

Hope it can help !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜