1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

why will changeIt("hello " world") choke javascript? (for a link)

Discussion in 'JavaScript' started by winterheat, Apr 6, 2009.

  1. #1
    I wonder why the following code will choke Javascript?

    (so the first link works, but the second doesn't) Thanks.


    <a href="#" onclick='changeIt("hello world"); return false;'>Click me</a>
    <a href="#" onclick='changeIt("hello &quot; world"); return false;'>Click me</a>    
    <div id="divContent"></div>  
    
      
    <script>
    
    function changeIt(title) {
    	document.getElementById('divContent').innerHTML = title + " " + new Date();	
    }
    
    </script>
    Code (markup):
     
    winterheat, Apr 6, 2009 IP
  2. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    on the other hand, this will work:

    <a href="#" id="link1">Click me</a>
    <a href="#" id="link2">Click me</a>    
    <div id="divContent"></div>  
    
      
    <script>
    
    document.getElementById('link1').onclick = function() {
    	changeIt("hello world"); return false;
    }
    
    document.getElementById('link2').onclick = function() {
    	changeIt("hello &quot; world"); return false;
    }
    
    function changeIt(title) {
    	document.getElementById('divContent').innerHTML = title + " " + new Date();	
    }
    
    </script>
    Code (markup):
     
    winterheat, Apr 6, 2009 IP
  3. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think the problem is that the &quot; is not enclosed in <script> tags and is being converted by the browser's HTML parser before it hits Javascript.

    The second example works because &quot; is inside the <script> tags
     
    joep1978, Apr 7, 2009 IP
  4. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    so it looks like

    <a href="#" onclick='changeIt("hello &quot; world"); return false;'>Click me</a>

    is the same as

    <a href="#" onclick='changeIt("hello " world"); return false;'>Click me</a>

    and for sure that will choke. i think someone told me that any value part of an attribute is first parsed as HTML. I just can't find where this is formally stated as a rule.
     
    winterheat, Apr 7, 2009 IP
  5. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you use Firebug and inspect the element that's exactly what you'll see. I'm afraid I don't know where you'd find this formally stated but it makes sense - the document has to be parsed first as HTML for the browser to know which bits are Javascript
     
    joep1978, Apr 7, 2009 IP
  6. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    really? does that mean

    onclick=&#039;changeIt("hello &quot; world");&#039;

    will also work? since &#039; is the single quote
    but i think it won't work as i tried the &quot; version with single quote inside and it didn't work

    onclick=&quot;changeIt('hello');&quot;
     
    winterheat, Apr 7, 2009 IP
  7. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No that causes a different problem - the &quot breaks the HTML parser which is looking for a closing ' to the onclick attribute
     
    joep1978, Apr 7, 2009 IP
  8. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i meant to type in & #039; for the single quote... but i think the forum program changed it to the single quote...

    the real thing i was wondering is

    <a href="#" onclick=&quot;alert(1);&quot;>Click me</a>

    will work too? it seems like it will not work. is only the value part of the attribute being parsed as HTML? thanks.
     
    winterheat, Apr 7, 2009 IP
  9. HolySavior

    HolySavior Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    why not just use " or ' and just pretty much escape it like \" or \' inside the function
     
    HolySavior, Apr 7, 2009 IP