Bug Tracker

Modify

Ticket #4533 (closed bug: invalid)

Opened 4 years ago

Last modified 15 months ago

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:
Blocking: Blocked by:

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

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

Change History

Changed 4 years ago by kspeakman

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

comment:1 Changed 4 years ago by dmethvin

Related: #5319

comment:2 Changed 3 years ago by miketaylr

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

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

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.