JavaScript
Article
By Sam Deering

jQuery Undetermined String Literal Error

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

Have you ever come upon this annoying error message: “undetermined string literal”.

undetermined-string-literal

Ok, i’ve got 3 occasions were you might run into this error and how to fix it for each specific case.

  1. Multiple Line Strings
  2. Wrong String Format (Bad Characters)
  3. Angle Brackets

Multiple Line Strings

If your trying to assign a string that covers multiple lines to a variable you might see the “Undetermined String Literal” error. To solve this you must use the JavaScript escape character backslash (“”) after each line to tell the interpreter where the line ends and to join the string together.

Another way is to simply split your string up into bits and add them together.

--ADVERTISEMENT--

Wrong String Format (Bad Characters)

If your trying to assign HTML to a variable you might see the “Undetermined String Literal” error. It might be that you loaded the HTML from somewhere else via ajax and are now trying to use/inspect it. Use the following code to clean up the bad characters in the string before trying to assign it.

Angle Brackets

If your trying to use angle brackets (“>” & “first 10 location like “abc”‘;
//note: abc replaces the actual query
[/js]

It turns out it was the securequery angle brackets creating the error and I had to hack it (as such) by adding in the angle brackets (““) just before encoding and sending the request via AJAX (see below).

$.ajax({
  type: 'POST',
  url: '/ajax/abc',
  data: 'content=' + encodeURIComponent(''),
  dataType: 'html',
  success: function(data){
	console.log(data);
	//display results
	$('#results').html(data);
  }
});

This might even be a bug in jQuery, not sure though. When you test in firebug it works though! So maybe not…

var fine = 'first 10 location like "abc"';
console.log(fine);
Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?