简体   繁体   English

对 href 使用 JavaScript 单引号和双引号

[英]Using JavaScript single and double quotes for href's

I am having problem with escaping the single and double quotes inside the href s JavaScript function.我对 escaping 有问题, href s JavaScript function 内的单引号和双引号。

I have this JavaScript code inside href .我在href中有这个 JavaScript 代码。 It's like -就像是 -

<a href = "javascript:myFunc("fileDir/fileName.doc", true)"> click this </a>

Now, since double quotes inside double quote is not valid, I need to escape the inner double quotes for it to be treated as part of the string - so, I need to do this -现在,由于双引号内的双引号无效,我需要转义内部双引号以将其视为字符串的一部分 - 所以,我需要这样做 -

<a href = "javascript:myFunc(\"fileDir/fileName.doc\" , true)"> click this </a>

The problem is, even the above code is not working.问题是,即使上面的代码也不起作用。 The JavaScript code is getting truncated at -- myFunc( JavaScript 代码在 -- myFunc(

I tried with the single quote variation too - but even that doesn't seem to work (meaning that if I have a single quote inside my string literal then the code gets truncated).我也尝试使用单引号变体 - 但即使这样似乎也不起作用(这意味着如果我的字符串文字中有单引号,那么代码会被截断)。

This is what I did with a single quote:这就是我用单引号所做的:

<a href = 'javascript:myFunc("fileDir/fileName.doc" , true)'> click this </a>

This works, but if I have a single quote inside the string then the code gets truncated in the same way as that of double quotes one.这可行,但如果我在字符串中有一个单引号,那么代码会以与双引号相同的方式被截断。

Using backslashes to escape quotes is how it works in JavaScript, but you're not actually writing JavaScript code there: you're writing HTML.使用反斜杠转义引号是它在 JavaScript 中的工作方式,但您实际上并没有在那里编写 JavaScript 代码:您正在编写 HTML。 You can do it by using the HTML escaping method: character entities.您可以使用 HTML escaping 方法来做到这一点:字符实体。

&quot;  // "
&#39;   // '

For example:例如:

<a href="javascript: alert('John O&#39;Brien says &quot;Hi!&quot');">...</a>

In case anyone needs to escape some thing like this:如果有人需要逃避这样的事情:

<a href="www.google.com/search?q="how+to+escape+quotes+in+href""</a>

You can use ASCII code for double quotes %22 :您可以对双引号%22使用 ASCII 代码:

<a href="www.google.com/search?q=%22how+to+escape+quotes+in+href%22"</a>

It is especially useful if you pass the link to JavaScript from PHP如果您从 PHP 传递到 JavaScript 的链接,这将特别有用

As a general best practice, use double-quotes in HTML and single-quotes in JavaScript.作为一般的最佳实践,在 HTML 中使用双引号,在 JavaScript 中使用单引号。 That will solve most of your problems.这将解决你的大部分问题。 If you need a single-quote in a JavaScript string, you can just escape it using \' - and you probably shouldn't be nesting literal strings any deeper than that.如果您需要 JavaScript 字符串中的单引号,则可以使用 \' 对其进行转义 - 您可能不应该将文字字符串嵌套得更深。

As noted elsewhere, HTML entities are a possibility if the code is embedded in HTML.如其他地方所述,如果代码嵌入在 HTML 中,则可能存在 HTML 实体。 But you'll still have to deal with escaping quotes in strings in your JavaScript source files, so it's best to just have a consistent strategy for dealing with JavaScript.但是您仍然必须处理 JavaScript 源文件中字符串中的 escaping 引号,因此最好采用一致的策略来处理 JavaScript。

If you are following this strategy and end up with a double-quote embedded in your JavaScript embedded in your HTML, just use the HTML entity &quot;.如果您遵循此策略并最终在 HTML 中嵌入 JavaScript 中嵌入双引号,则只需使用 HTML 实体“。

Normally, this kind of code is working without problems:通常,这种代码可以正常工作:

<a href="#" onclick="myFunc('...')">Click this</a>

With this code, do you have any problem?有了这段代码,你有什么问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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