2016-11-10 47 views
0

什么是最好的方式将URL附加到查询字符串在JavaScript中?我意识到它需要被编码。编码的URL,所以它可以发送通过GET

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

用法示例:

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

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

firstURL+someURL; 
+0

是encodeURIComponent将提供正确的编码。 –

+0

encodeURIComponent是此功能的正确功能。 – Gerfried

回答

1

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

相关问题