谁还没个明天 2008-12-02 02:37 采纳率: 66.7%
浏览 383
已采纳

在 JavaScript 中编码 URL?

How do you safely encode a URL using JavaScript such that it can be put into a GET string?

var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;

I assume that you need to encode the myUrl variable on that second line?

转载于:https://stackoverflow.com/questions/332872/encode-url-in-javascript

  • 写回答

13条回答 默认 最新

  • 旧行李 2008-12-02 02:43
    关注

    Check out the built-in function encodeURIComponent(str) and encodeURI(str).
    In your case, this should work:

    var myOtherUrl = 
           "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?