Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript parsing error...

    Hello, the following is a bit of javascript code that my PHP code generates.
    The browser complains saying that: "Expected ')'":
    Code:
    <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:
    Code:
    <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

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by davidklonski
    Hello, the following is a bit of javascript code that my PHP code generates.
    The browser complains saying that: "Expected ')'":
    Code:
    <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:
    Code:
    <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')">

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, that helped me solve the problem


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •