Skip to main content

Bug Tracker

Side navigation

#4533 closed bug (invalid)

Opened April 13, 2009 11:28PM UTC

Closed October 21, 2010 07:23PM UTC

Last modified March 13, 2012 05:35PM UTC

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 (0.4 KB) - added by kspeakman April 13, 2009 11:29PM UTC.

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

Change History (2)

Changed October 03, 2009 12:32AM UTC by dmethvin comment:1

Related: #5319

Changed October 21, 2010 07:23PM UTC by miketaylr comment:2

resolution: → invalid
status: newclosed

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