...

View Full Version : escaping "quotes" and <arrows> in a function



kakshmire
06-06-2006, 01:37 PM
hi. i have this code which displays links. When the link is clicked it adds text to a text area.


<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 :confused:

Kor
06-06-2006, 02:22 PM
Use unicode escapes


<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

kakshmire
06-06-2006, 03:43 PM
worked a treat :thumbsup: doesnt seem to like me not leavign spaces between each unicode but that shouldn't matter. many thanks.

kakshmire
06-07-2006, 12:01 AM
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...

Kor
06-07-2006, 09:00 AM
Your problem is that you should have used value instead of innerHTML. And don't leave gaps between <textarea> and </textarea>


<!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>

kakshmire
06-07-2006, 11:42 AM
excellent :D works definately now :) thanks.

brothercake
06-07-2006, 05:59 PM
< and > symbols are not arrows - http://www.udm4.com/manual/depth/indicators/

Kor
06-07-2006, 09:41 PM
.. nor naming variables the same as the id or as the javascript reserved words, but this is another theme...

brothercake
06-08-2006, 12:14 PM
.. nor naming variables the same as the id
There's nothing wrong with doing that, if they're not in the same scope ;)

Kor
06-08-2006, 12:22 PM
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

brothercake
06-08-2006, 01:35 PM
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.

Kor
06-08-2006, 02:02 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum