简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM