Bug Tracker

Modify

Ticket #48 (closed bug: duplicate)

Opened 7 years ago

Last modified 6 years ago

Seach scope does not work as expected with content loaded via $.get

Reported by: Enchos@… Owned by:
Priority: minor Milestone:
Component: ajax Version:
Keywords: Cc:
Blocking: Blocked by:

Description

I tried to load content from another file via $.get, find an element inside that loaded content and put that inside the current document. eg. $.get("url", function(content){

var content = $("#stuff", content).html(); do something with content

});

In my case, the id to search for is already in the document. As a result, $("#stuff", content).html() returns the content of #stuff inside the current document, not from the content loaded via get. It looks like a bug with the search scope. I put up an example page to demonstrate the problem:

 http://bassistance.de/crap/scopebug/bug1.html

Click the link to bug2.html to see what should be loaded into the red box.

Change History

comment:1 Changed 7 years ago by john

  • Version set to 1.0
  • Milestone set to 1.0

comment:2 Changed 7 years ago by john

  • Priority changed from critical to minor
  • Type changed from bug to enhancement

This is especially tricky, considering that XML documents don't support the ID selector naturally - or innerHTML, for that matter. I'm going to file this as an enhancement and deal with it someday.

comment:3 Changed 7 years ago by frohoff@…

  • Type changed from enhancement to bug

I've noticed this happening with element selectors as well:

$.get("url", function(content){

var content = $("title", content).text(); do something with content

});

this code obtains the current document's title instead of the retrieved document's title.

comment:4 Changed 7 years ago by frohoff@…

  • Priority changed from minor to major

Actually, these alerts don't even give you the same result; the first one displays the requested page's markup, while the second one displays the current page's markup.

$.get("url", function(xml){

alert(xml); alert($(xml).html());

});

comment:5 Changed 7 years ago by john

  • Priority changed from major to minor
  • Version 1.0 deleted
  • Type changed from bug to enhancement
  • Milestone 1.0 deleted

There are two different problems discussed in this report - the one originally posted about and the one supplied by frohoff. I'll talk about both.

1) Currently, this is not possible for the simple reason that XML documents, unless you've specified an explicit DTD, don't have an getElementById selector. Both #foo and .html() fail on non-HTML-DOM documents. This is a known issue, but one that probably won't be addressed until post-1.0.

It is for this reason that I'm going to re-classify this as an enhancement.

2) The second issue is with the content-type of the document. You must make sure that the document being returned from the server has the correct content type associated with it (explicitly, Content-type: text/xml). If, for some reason, you cannot change the header, and it is wrong, try this instead:

$.get("url", callback, "xml")

and it will try to parse it as an XML document instead.

comment:6 Changed 7 years ago by anonymous

  • Status changed from new to closed
  • Resolution set to wontfix

comment:7 Changed 7 years ago by john

  • Status changed from closed to reopened
  • Resolution wontfix deleted

comment:8 Changed 7 years ago by john

  • Status changed from reopened to closed
  • Type changed from enhancement to bug
  • Resolution set to duplicate

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.