...

View Full Version : javascript parsing error...



davidklonski
06-23-2004, 01:22 AM
Hello, the following is a bit of javascript code that my PHP code generates.
The browser complains saying that: "Expected ')'":


<img src="face.gif" onmouseover="func('Guy& #039;s name')">

I intentionally wrote a space between the '&' and the '#' so that the browswer won't convert it to ' when you view the message.

The content of the string that will be passed to the func function is generated dynamically by PHP and unfortunately it may contain escape characters such as ', ".

I am using a PHP function to convert all applicable characters in the string to HTML entities.
I thought that using this trick will allow me to have nesting of quotes and double quotes.

Otherwise, the final javascript code would look like:


<img src="face.gif" onmouseover="func('Guy's name')">

and this obviously doesn't work.

What is the best way of handling such cases in Javascript (putting aside server-side scripting)?

thanks

Willy Duitt
06-23-2004, 01:28 AM
Hello, the following is a bit of javascript code that my PHP code generates.
The browser complains saying that: "Expected ')'":


<img src="face.gif" onmouseover="func('Guy& #039;s name')">

I intentionally wrote a space between the '&' and the '#' so that the browswer won't convert it to ' when you view the message.

The content of the string that will be passed to the func function is generated dynamically by PHP and unfortunately it may contain escape characters such as ', ".

I am using a PHP function to convert all applicable characters in the string to HTML entities.
I thought that using this trick will allow me to have nesting of quotes and double quotes.

Otherwise, the final javascript code would look like:


<img src="face.gif" onmouseover="func('Guy's name')">

and this obviously doesn't work.

What is the best way of handling such cases in Javascript (putting aside server-side scripting)?

thanks

Try escaping the single quote with a backslash.
<img src="face.gif" onmouseover="func('Guy\'s name')">

davidklonski
06-23-2004, 01:41 AM
thanks, that helped me solve the problem



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum