将URL编码的字符串分配给href
时,为什么未编码的"
个字符已编码? this jsfiddle中对此进行了演示。我知道"
是网址中的有效字符,但是我不理解为什么在这种情况下encodeURIComponent()
会对其进行完全编码,或者为什么href
本身不会对其进行编码好像编码版本%22
也是有效的,并且可以在URL中理解。还是取消编码是由浏览器动态完成的(只是为了提高可读性)?我正在使用Chrome。
<!DOCTYPE html>
<html>
<body>
<h2>Fake Links</h2>
<p>This is just a page with a <a href='#' id='link'>fake link</a> where the content of the fake link is also below... but why are double quote characters encoded in the raw output below but unencoded in the fake link itself (above)?</p>
<textarea id='text_area' style='width: 100%; height: 300px; font-family: monospace;'></textarea>
<script>
var json = { a: 2, b: 'x' };
var encoded = encodeURIComponent(JSON.stringify(json));
var url = 'https://example.com/fakeLink/' + encoded;
document.getElementById('link').href = url;
document.getElementById('text_area').value = url;
</script>
</body>
</html>