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 7 of 7
  1. #1
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post

    JavaScript quotation marks and apostrophes

    Hello,

    Code:
    <button onclick="javascript:document.write('hello');"></button>
    
    <button onclick="javascript:document.write("hello");"></button>
    
    <button onclick='javascript:document.write('hello');'></button>
    
    <button onclick='javascript:document.write("hello");'></button>
    Does it matter putting quotation marks or apostrophes when using JavaScript?
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    No to JavaScript, assuming they are correctly matched up.

    Same answer for HTML.

    BUT... But if you embed javascript *in* HTML, then HTML will process the quotes first.

    For example, HTML will see the strings I show in red:
    Code:
    <button onclick="javascript:document.write("hello");"></button>
    
    same as if you had written
    
    <button onclick="javascript:document.write("
       hello
       ");"
    ></button>
    So the only code given to javascript will be document.write(

    The hello and the ); are then HTML errors, but of course HTML just ignores errors.

    NOTE: You can't use document.write to respond to *ANY* human interaction.
    NOTE: The use of javscript: like that was deprecated more than 10 years ago.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,677
    Thanks
    6
    Thanked 1,008 Times in 981 Posts
    Quote Originally Posted by Old Pedant View Post

    For example, HTML will see the strings I show in red:
    Code:
    <button onclick="javascript:document.write("hello");"></button>
    
    same as if you had written
    
    <button onclick="javascript:document.write("
       hello
       ");"
    ></button>
    So the only code given to javascript will be document.write(

    The hello and the ); are then HTML errors, but of course HTML just ignores errors.
    To expand on that for clarification: for HTML the first double quote is the opening quote of the attribute value and the second double quote it encounters is the closing double quote:
    Code:
    <button onclick="javascript:document.write("hello");"></button>
    Since the rest coming after that second double quote isn’t a valid attribute it’s an error.

    Now, you shouldn’t write inline JS anyway but it’s still good to know that if you do that then you should use alternating single/double quotes, i. e.:
    Code:
    onclick="here comes JS and 'single quotes' should be used inside here"
    onclick='here comes JS and "double quotes" should be used inside here'

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Well, I didn't realize we were going to explore this in depth, but since we are...

    Don't forget about this:
    Code:
    <input type="button" onclick="alert(&quot;this works, too&quot;)" value="see?"/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post
    I appreciate all the suggestions, the development software I have to aid me is probably deprecated itself; but I do understand a tad more about how the double and single quotations/ apostrophes functions.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Which is easier to read? alert("iBall's a troll"); or alert('iBall\'s a troll');

    Double quotes involve pressing the shift key.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,677
    Thanks
    6
    Thanked 1,008 Times in 981 Posts
    Quote Originally Posted by Philip M View Post
    Double quotes involve pressing the shift key.
    So do single quotes for me.


  •  

    Tags for this Thread

    Posting Permissions

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