homepage Welcome to WebmasterWorld Guest from 184.73.104.82
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

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4573759 posted 11:02 pm on May 13, 2013 (gmt 0)

What is the bug? What happens?

DrDoc

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4573759 posted 11:40 pm on May 13, 2013 (gmt 0)

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

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4573759 posted 5:13 am on May 14, 2013 (gmt 0)

Does $(data) alone fail ?

DrDoc

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4573759 posted 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

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4573759 posted 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