View Full Version : How to escape quotes in this code?

10-08-2011, 11:26 AM
<div id="link">
<a href="http://www.facebook.com/">Facebook</a>
<script type="text/javascript">

var addr=new Array();

var rl=document.getElementById("link");
var ri=Math.floor(Math.random()*3);



I got stuck in getting this to work (show random element of the array as a link with description)

rl.innerHTML='<a href="addr[ri]">addr[ri]</a>';

This is showing addr[ri] on a page and directs to www.domain.com/addr[ri]

Thank you in advance for any info.

10-08-2011, 11:49 AM
This doesn't have anything to do with escaping quotes but rather with string concatenation (the + operator). What you want to do is concatenate the following strings

1. <a href="
2. The content of addr[ri]
3. ">
4. The content of addr[ri]
5. </a>

Between each part of those strings you will have to use the + operator

rl.innerHTML='<a href="' + addr[ri] + '">' + addr[ri] + '</a>';

10-08-2011, 11:55 AM
Thanks, this is working now...

10-08-2011, 01:11 PM
Escape characters are part of the syntax for many programming languages, data formats and communication protocols. For a given alphabet an escape character's purpose is to start character sequences (so named escape sequences) which have to be interpreted differently from the same characters occurring alone. An escape character may not have its own meaning, so all escape sequences are of 2 or more characters.
There are usually two functions of escape sequences. The first is to encode a syntactic entity, such as device commands or special data which cannot be directly represented by the alphabet. The second use, referred to as character quoting, is to represent characters which cannot be typed in current context, or would have an undesired interpretation. In the latter case an escape sequence is a digraph consisting of an escape character itself and a "quoted" character.