编码 URL 以便可以通过 GET 发送

encode URL so it can be send through GET

将 URL 附加到 javascript 中的查询字符串的最佳方法是什么?我意识到它需要编码。

我遇到了 encodeURIComponent() 函数,它看起来像我想要的东西。我只是不确定它是否适合这种任务。

用法示例:

var someURL = encodeURIComponent("http://whosebug.com/questions/ask?name=.hil#");

var firstURL = "www.whosebug.com/questions?someurl="

firstURL+someURL;

您的选择是 encodeURIencodeURIComponentencodeURIComponent 是正确的选择,因为您正在对 URL 的一部分进行编码(恰好类似于 URL,但这在这里并不重要)。 如果您要使用 encodeURI,它不会转换组件中足够的字符。