简体   繁体   English

为什么对href属性使用url编码而不是html编码?

[英]Why use url encoding instead of html encoding for the href attribute?

In this bit.ly bookmark I see they are using url encoding 在这个bit.ly书签中,我看到他们正在使用url编码

  <a title="✚ Bitlink" href="javascript:(function()%20%7B%20var%20s%20=%20document.createElement(%22script%22);%20s.setAttribute(%22id%22,%20%22bitmark_js%22);%20s.setAttribute(%22type%22,%20%22text/javascript%22);%20s.setAttribute(%22src%22,%20%22//bitly.com/a/bitmarklet.js%22);%20(top.document.body%20%7C%7C%20top.document.getElementsByTagName(%22head%22)[0]).appendChild(s);%20%7D)();" id="bitmarklet">✚ Bitlink</a></p>

However as it is actually JavaScript and not going to be processed by the URL agent I don't understand why they chose this encoding. 但是,因为它实际上是JavaScript,并且不会由URL代理处理,所以我不明白他们为什么选择这种编码。

Normally an href would have a GET request which would need url encoding, but not in this case. 通常,href会有一个GET请求,该请求需要url编码,但在这种情况下不需要。

Why is this? 为什么是这样?

They do that because this is an a tag. 他们这样做是因为这是a标签。 When clicked the href will be parsed by the browser's URL parser. 单击href时,浏览器的URL解析器将解析href Anything that is parsed as a URL needs to be URL encoded. 解析为URL的任何内容都需要进行URL编码。 In this case it is indeed simply JavaScript and the browser will execute it as such, but the parser still needs an URL encoded string to not choke on it. 在这种情况下,确实确实是JavaScript,浏览器将照此执行它,但是解析器仍然需要URL编码的字符串才能阻止它。

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

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