Lei-Lonnie
Lei-Lonnie

Reputation: 784

How do you use multiple quotes in javascript?

What do you do when you already have single and double quotes in a URL, but then you need to wrap that URL in quotes?

For example:

<script src="http://(some url text)xpath='//*[@id="node-1075"]/div/div[1]/div/div/p[2]'"></script>

The node ID is wrapped in quotes, if I put the URL alone in the address bar it works, but as soon as it gets wrapped in quotes it doesn't, I can't escape the quotes either or else it will fail, what do I do?

Upvotes: 1

Views: 3053

Answers (2)

user4695271
user4695271

Reputation:

You need to escape them; usually you write (inside HTML files) double quotes first, then single quotes (the opposite in .js files, but that's my personal style); whenever you need the same in between you need to escape it.

Example:

document.getElementById("some").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";

Notice that using the JavaScript escape character (\) isn't enough in an HTML context; you need to replace the double-quote with the proper XML entity representation, &quot;.

Example:

<a href="#" onclick="doIt('The &quot;Thing&quot;');">Do It!</a>

In your case, I would recommend to URL encode "the URL" though.

Upvotes: 1

Mel D
Mel D

Reputation: 41

You should be able to replace the double quotes with: %22 And the single quotes with: %27

So your URL would be:

"http://(some url text)xpath=%27//*[@id=%22node-1075%22]/div/div[1]/div/div/p[2]%27"

Here is the complete list of ASCII Encoding http://www.w3schools.com/tags/ref_urlencode.asp

Upvotes: 4

Related Questions