Special Charators

Hi Folks,

Is there a way to use them in strings?

Can I search for them and replace them with an ascii value or silimar somehow?

Thanks in advace

I don’t understand. :frowning:
If it’s from an input field it’s already a string!

Do you mean the text comes from a database and you’re creating JavaScript containing that text using server-side programming? In that case you need to escape special characters in the server-side script that generates the code. It’s not a JavaScript problem.

Why should that cause an error (unless homepage[0] is undefined)?

When asking for help online it’s always a good idea to be as explicit as possible about what you’re trying to do, and why. Providing code examples of existing code also helps. I don’t quite understand what you are trying to do, or how.

Thanks for your reply.

Its from a user input field so I’m not sure where the special chars will be and what they are.

I query the database and create my html, could I use a regular expression or similar when doing this to convert all the special chars to javascript friendly text?

For example this code causes an error:

homepage[0] += “example - example”

Many thanks

What special characters? If you want to include a character that you cannot input directly from your keyboard, or cannot be represented using the character encoding you use, you can use escape notation.

Octal escape notation:

var copyright = "\\251 2010";

Unicode escape notation:

var copyright = "\\u00a9 2010";

Literal notation:

var copyright = "© 2010";

For characters that are special to JavaScript, escape them with a preceding backslash:

var s1 = "My \\"new\\" car";
var s2 = 'Jane\\'s car';

Thanks for your reply.

The javascript thats causing an error is below.

Can you help?


homepage[0] = "<h2 class='draggable'>Bring n buy</h2><div class='container'>"
homepage[0] += '<ul>'
homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'>Kingsize bed frame</a></li>"

Error: unterminated string literal
Source File: http://intranet/portal.cfm
Line: 24, Column: 16
Source Code:
 homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'> 

Do you have this JavaScript code between <script> and </script> rather than in an external script file (<script src="..."></script>?

If so, the problem is most likely the closing tags you have in your strings.

In HTML (which includes pretend-XHTML) the script element type has a content model of CDATA, which behaves a bit oddly. One oddity is that the first occurrance of the character sequence ‘</’ followed by a name-start character will be interpreted as </script> and thereby close the script tag.

To prevent this you need to escape that character sequence, which you can do by inserting a backslash character between the ‘<’ and the ‘/’. That ‘escapes’ the ‘/’ character.

homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'>Kingsize bed frame<\\/a><\\/li>"

Without the backslash characters in the end tags, the script tag is closed after ‘bed frame’ which means the string constant isn’t properly terminated.