开发者

Proper encoding of href for window.open() in JavaScript

What is the proper cross-browser encoding for the href when using window.open() in JavaScript? First I was using

var href = "http://127.0.0.1:8000/etf/admin/escola/t34atividade/?pop=1&copy=1";
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');

IE8 opens: http://127.0.0.1:8000/etf/admin/escola/t34atividade/?pop=1©=1

FireFox opens: http://127.0.0.1:8000/etf/admin/escola/t34atividade/?pop=1&copy=1

var href = "http://127.0.0.1:8000/etf/admin/escola/t34atividade/?pop=1&copy=1";
var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');

IE8 opens: http://127.0.0.1:8000/etf/admin/escola/t34atividade/?pop=1&copy=1

FireFox opens: http://127.0.0.1:8000/etf/adm开发者_如何转开发in/escola/t34atividade/?pop=1&copy=1


Use the Javascript "encodeURIComponent" function for each piece of the URI that's not part of the URI syntax (that is, separator slashes, the question mark for the query string, parameter separator ampersands, etc).

URI encoding is not the same as HTML escaping. For example, you don't escape an ampersand in a URL as &.


IE8 appears to be trying to coerce the query string argument &copy=1 to the entity ©, which is the copyright symbol (©). That is actually funny. Just like Microsoft to encumber the user with "help".

Pointy is correct about encoding. Be careful also that you don't have a code minifier that removes everything on a line after a pair of double slashes (//); I've seen those wreck pages before.


The simplest solution I found was to stop using copy as a GET parameter. The problem is that &copy is actually an HTML entity for the copyright symbol. IE applies an entity replacement converting it to the symbol even though it's in JavaScript code. Apparently Firefox does not perform the entity replacement. According to a comment in this blog what IE is doing might be correct, but to avoid all the mess I just renamed my parameter to clone.

http://nedbatchelder.com/blog/200812/accidental_html_entities_in_urls.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜