如何安全地使用JavaScript对URL进行编码,以便将其放入GET字符串中?
var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
我猜您需要在第二行对myUrl
变量进行编码?
请查看内置函数encodeURIComponent(str)和encodeURI(str)。
在这种情况下,这应该可以工作:
var myOtherUrl =
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);