SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing double quotes to javascript function

    Hi all,
    I am trying to pass variable to a function with nested double quotes.
    How do I escape the double quotes?

    ...
    function change(str1){
    document.write(str1);
    }
    ...
    <TD onmouseover="javascript:change('My name is \"Tom\"')" onmouseout="javascript:change('')">Some text</td>
    ...
    This is not working. Any ideas?

    Any help would be appreciated.
    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It's kind of a strange problem. Triple nesting of quotes isn't possible, so you would think escaping the double quotes around Tom would work, but it doesn't. As far as I can tell, the rule is: you can only escape quotes that are the same as the next set of quotes surrounding that part of the string.

    So, one solution is to start with single quotes:

    onmouseover='change("My name is \"Tom\".")'

    Or, strangely enough, what also works is using the html symbol for a quote: &#34

    onclick="g('My name is &#34Tom&#34.')"

    but that is an ugly solution.

    Question. Why do you have:

    javascript:

    before the function name?

  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much for your reply!

    Quote Originally Posted by 7stud
    Triple nesting of quotes isn't possible, so you would think escaping the double quotes around Tom would work, but it doesn't. As far as I can tell, the rule is: you can only escape quotes that are the same as the next set of quotes surrounding that part of the string.

    So, one solution is to start with single quotes:
    onmouseover='change("My name is \"Tom\".")'

    Or, strangely enough, what also works is using the html symbol for a quote: &#34
    onclick="g('My name is &#34Tom&#34.')"
    The problem with starting with single quotes is that now I will not be able to use a single quote in the string I am outputting. I have hardcoded this example because I assumed there was a simple solution.

    What I am trying to do on my website is: allow a user to enter any values(double and single quotes included) into an html text area - then save that info in a db using php - and finally, pass the user's text (contained in a php variable) to a javascript function for displaying in another area of the webpage(during a mouseover event).

    This is also why using the html symbol for the quote is not really good either. I could write a function in php to change to the html symbols, but I was really looking for another solution.

    Quote Originally Posted by 7stud
    Question. Why do you have:
    javascript:
    before the function name?
    Sorry to confuse you - that was a typo.
    Thanks again for the help.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I am trying to do on my website is: allow a user to enter any values(double and single quotes included) into an html text area - then save that info in a db using php - and finally, pass the user's text (contained in a php variable) to a javascript function for displaying in another area of the webpage(during a mouseover event).
    Try writing the data into the escape() function, which is like the php urlencode() function:

    var text = escape(<?php echo $data; ?>);

    Then, use unescape() when you want to display the text:

    document.getElementById("someID").value = unescape(text);


Bookmarks

Posting Permissions

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