Bug Tracker

Opened 11 years ago

Closed 9 years ago

Last modified 8 years ago

#4533 closed bug (invalid)

text(val) and new String

Reported by: kspeakman Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: text(val) Cc:
Blocked by: Blocking:

Description

text(val) fails if val is a Javascript String object. This is counter-intuitive, and hard to track down. The default error message when this happens is not meaningful. Firefox says "G is undefined". Chrome says "Uncaught TypeError: Cannot read property 'length' of undefined". Safari says "Undefined value". IE says "'length' is null or not an object".

Repro

Repro code follows and is also attached:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	 <head>
	 	 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
	 </head>
	 <body>
	 	 <input type="button" value="Crash!" onclick="$('#test').text(new String('Test2'));" />
	 	 <span id="test">Test</span>
	 </body>
</html>

Fix

Suggested fix follows. I haven't looked at the text(val) code, but in my imagination a fix would look something like this:

text: function(val) {
	 if (val.toString !== undefined) val = val.toString();
	...
}

Attachments (1)

bug.jQuery.text.html (396 bytes) - added by kspeakman 11 years ago.
Demo page for the text(val) and Javascript String object bug.

Download all attachments as: .zip

Change History (3)

Changed 11 years ago by kspeakman

Attachment: bug.jQuery.text.html added

Demo page for the text(val) and Javascript String object bug.

comment:1 Changed 10 years ago by dmethvin

Related: #5319

comment:2 Changed 9 years ago by miketaylr

Resolution: invalid
Status: newclosed

.text() takes a string primitive as an argument (http://api.jquery.com/text/), rather than a string object.

Note: See TracTickets for help on using tickets.