cade galt
cade galt

Reputation: 4161

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?

Upvotes: 2

Views: 761

Answers (1)

Koen Peters
Koen Peters

Reputation: 12916

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.

Upvotes: 2

Related Questions