在JavaScript中编码URL

内容来自 DOC https://q.houxu6.top/?s=在JavaScript中编码URL

如何安全地使用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);

posted @ 2023-10-31 19:58  小满独家  阅读(13)  评论(0)    收藏  举报