homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Interesting bug in jQuery 2.0
DrDoc




msg:4573761
 10:53 pm on May 13, 2013 (gmt 0)

I have a standard $.ajax() call in my script. It requests HTML (in this case an HTML table row) from a given URL and then inserts it somewhere on the page.

The returned string sometimes contains newline characters or tabs. Prior to version 2.0, both of these worked:

$(data).appendTo(elem);
$(data).prependTo(elem);

After upgrading to jQuery 2.0, newlines and tabs must be removed. Simply replacing them with spaces still triggers the same bug (even though there are spaces elsewhere in the string which work fine).

Interesting, and annoying ...

 

Fotiman




msg:4573764
 11:02 pm on May 13, 2013 (gmt 0)

What is the bug? What happens?

DrDoc




msg:4573780
 11:40 pm on May 13, 2013 (gmt 0)

jQuery throws an error saying "[contents of data] is not a function".

daveVk




msg:4573896
 5:13 am on May 14, 2013 (gmt 0)

Does $(data) alone fail ?

DrDoc




msg:4574072
 3:20 pm on May 14, 2013 (gmt 0)

Ok, did some more testing.

On really long strings, series of \t\r\n present in $(data) may trigger the error. Also, the presence of anything that looks like a JS comment (//, /*, */) may trigger a different error (unrecognized expression).

In both cases, $(data) alone fails.

Fotiman




msg:4574076
 3:39 pm on May 14, 2013 (gmt 0)

So, I see the same behavior with both 1.9 and 2.0. The behavior I see is that if the data is a complete HTML page (with doctype, etc.), then jQuery won't treat it as an element. That is, $(data).find("validSelector") will not find any elements. However, if I wrap the results in a <div></div> first, then it works.

Here's an example using jQuery 2.0:
[jsfiddle.net...]

And here's the same example using jQuery 1.9.1:
[jsfiddle.net...]

They both work, but if I remove the wrapping div, they both fail.

daveVk




msg:4574269
 5:51 am on May 15, 2013 (gmt 0)

Try $.parseHTML()

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it starts with <tag ... >).
...
For explicit parsing of a string to HTML, use the $.parseHTML() method.


[api.jquery.com...]

Fotiman




msg:4574411
 1:34 pm on May 15, 2013 (gmt 0)

No good. That returns an array and then you end up with "Uncaught TypeError: Object [object Array] has no method 'find'"
[jsfiddle.net...]

DrDoc




msg:4574431
 3:29 pm on May 15, 2013 (gmt 0)

And in my case, it's a valid block of HTML (<div>...</div> or <tr>...</tr>) ...

Fotiman




msg:4574437
 3:58 pm on May 15, 2013 (gmt 0)

Any way you can get it down to a base case, since I can't seem to reproduce it?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved