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 12 of 12
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    escaping "quotes" and <arrows> in a function

    hi. i have this code which displays links. When the link is clicked it adds text to a text area.

    Code:
    <html>
    <head><title>Crazy test</title></head>
    <body>
    <form>
    	<textarea id="textarea" cols="30" rows="10">Already existsing text.
    	</textarea>
    </form>
    
    <a href="#" onclick="addLink('a href = /pages/events.php?cmd=view&id=1');">add id 1</a><br/>
    <a href="#" onclick="addLink('a href = /pages/events.php?cmd=view&id=3');">add id 3</a>
    
    <script type="text/javascript">
    function addLink(link)
    {
    	var textarea = document.getElementById("textarea");
    	textarea.innerHTML += link + "\n";
    }
    </script>
    </body>
    </html>
    this adds

    a href = /pages/events.php?cmd=view&id=#

    to the text area, but i need it to add

    <a href = "/pages/events.php?cmd=view&id=3">

    whenever i add < or " it escapes the function (i think) and adding \ to escape the character doesnt work. any ideas

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Use unicode escapes
    Code:
    <a href="#" onclick="addLink('\u003ca href=\u0022/pages/events.php?cmd=view&id=1\u0022\u003e')">add id 1</a><br/>
    <a href="#" onclick="addLink('\u003ca href=\u0022/pages/events.php?cmd=view&id=3\u0022\u003e');">add id 3</a>
    See also
    http://www.hot-tips.co.uk/useful/unicode_converter.HTML
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    worked a treat doesnt seem to like me not leavign spaces between each unicode but that shouldn't matter. many thanks.

  • #4
    New Coder
    Join Date
    May 2004
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, after actually trying it out for real, it turns out that i cant have a space.

    using the unicode for <a

    which is \u003c\u0061 just shows up blank, as does \u003ca.

    i tried doing 2 seperate addLink functions but still get a space in between...
    Last edited by kakshmire; 06-07-2006 at 12:09 AM.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Your problem is that you should have used value instead of innerHTML. And don't leave gaps between <textarea> and </textarea>
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function addLink(link)
    {
    	var textarea = document.getElementById("textarea");
    	textarea.value += link + "\n";
    }
    </script>
    </head>
    <body>
    <form>
    	<textarea id="textarea" cols="30" rows="10">Already existsing text.</textarea>
    </form>
    <a href="#" onclick="addLink('\u003ca href=\u0022/pages/events.php?cmd=view&id=1\u0022\u003e')">add id 1</a><br/>
    <a href="#" onclick="addLink('\u003ca href=\u0022/pages/events.php?cmd=view&id=3\u0022\u003e');">add id 3</a>
    </body>
    </html>
    Last edited by Kor; 06-07-2006 at 09:03 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    May 2004
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    excellent works definately now thanks.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    < and > symbols are not arrows - http://www.udm4.com/manual/depth/indicators/
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    .. nor naming variables the same as the id or as the javascript reserved words, but this is another theme...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    .. nor naming variables the same as the id
    There's nothing wrong with doing that, if they're not in the same scope
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    well, IE will bring into errors if you name a variable same as an element's id

    var foo='fee'
    ...
    <sometag id="foo">

    Now, as IE6 inherits a silly IE4 shortcut (it is able to reference the elements directly by id without document root), it will be confused between the variable foo and the element with id=foo
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    well, IE will bring into errors if you name a variable same as an element's id

    var foo='fee'
    ...
    <sometag id="foo">

    Now, as IE6 inherits a silly IE4 shortcut (it is able to reference the elements directly by id without document root), it will be confused between the variable foo and the element with id=foo
    Like I said - that's only a problem if the variable is in the same scope, ie, if it's in the window scope. The variable in this example is in a function scope, so there's no problem - it's not the same variable.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    anyway, to avoid any hidden conflict, I think is wiser to use different names. upper case (or camel case) is a good solution as well

    var Foo
    var textArea
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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