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 6 of 6
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    ’ works, ' doesn't

    Hello!

    I have a page where I use javascript to make some info appear in a box when I hover a link. This is a simplified version of my code:
    Code:
    <html>
    <head>
    <title>Apostrophe bug</title>
    <script type="text/javascript">
    <!--
    
    function desc(newText){
    
     document.getElementById('jsLinkDesc').innerHTML=newText;
    }
    
    -->
    </script>
    </head>
    <body>
    
    <ul>
      <li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
      <li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
      <li><a href="#" onmouseover="desc('This won't work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
    </ul>
    <div id="jsLinkDesc">Link Description Box</div>
    </body>
    </html>
    As you can see, the "Faulty link"'s onmouseover doesn't work because there is an apostrophe. So I tried changing the apostrophe with &#39;
    Code:
    <html>
    <head>
    <title>Apostrophe bug</title>
    <script type="text/javascript">
    <!--
    
    function desc(newText){
    
     document.getElementById('jsLinkDesc').innerHTML=newText;
    }
    
    -->
    </script>
    </head>
    <body>
    
    <ul>
      <li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
      <li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
      <li><a href="#" onmouseover="desc('This won&#39;t work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
    </ul>
    <div id="jsLinkDesc">Link Description Box</div>
    </body>
    </html>
    It still doesn't work!!
    I then tried with &#8217;
    Code:
    <html>
    <head>
    <title>Apostrophe bug</title>
    <script type="text/javascript">
    <!--
    
    function desc(newText){
    
     document.getElementById('jsLinkDesc').innerHTML=newText;
    }
    
    -->
    </script>
    </head>
    <body>
    
    <ul>
      <li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
      <li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
      <li><a href="#" onmouseover="desc('This won&#8217;t work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
    </ul>
    <div id="jsLinkDesc">Link Description Box</div>
    </body>
    </html>
    THIS works!!!

    What's up with all this?
    Thanks for any help


    While I'm posting up my script, I might as well ask a few more questions:
    • In my script, I use innerHTML. How could I do the same thing using DOM?
    • My script makes really ugly HTML, how could I make it cleaner? Idealy having something like: <a href="#">Link</a>. I could apply the onmouseover & onmouseout with an external .js file, but the content of each desc('content') is different. Any idea? Also, in the non-simplified version, I also have onfocus and onblur...

    Thank you
    Last edited by shlagish; 12-19-2004 at 08:48 PM.
    Shawn

  • #2
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    "desc('This won\'t work because of the apostrophe')"
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    MY HERO!

    Thank you, hadn't thought of that.

    What about the other questions?
    Shawn

  • #4
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Internet Explorer

    It’s a different type of quote.

    “Hello!” - word/publisher
    "Hello!" - computer/internet
    It’s different. - word/publisher
    It's different. - computer/internet

    You can use “word” quotes in JS because the JS only processes its code with "computer" quotes.
    <div> - putting your mind in a box since 1997

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    makes a whole lot of sence, but &#39; doesn't have any computer quote in it...
    Shawn

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    It's a question about parsing levels, really. If the error comes from the JavaScript parsing, then escaping it in the HTML doesn't help. The HTML parser will already have replaced it with the corresponding value when it is sent to the JavaScript parser.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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