简体   繁体   中英

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

In this bit.ly bookmark I see they are using url encoding

  <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.

Normally an href would have a GET request which would need url encoding, but not in this case.

Why is this?

They do that because this is an a tag. When clicked the href will be parsed by the browser's URL parser. Anything that is parsed as a URL needs to be URL encoded. 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.

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