Javascript 如何将具有多个参数的 URL 传递到 URL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5095887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a URL with multiple parameters into a URL?
提问by DormoTheNord
Basically I'm trying to pass a URL like this:
基本上我试图传递这样的 URL:
www.foobar.com/?first=1&second=12&third=5
into a URL like this:
进入这样的网址:
http://www.facebook.com/sharer.php?&t=FOOBAR&u=http://www.foobar.com/first=12&sec=25&position=2
It only recognizes the first parameter. I'm having the same problem with LinkedIn and Twitter sharing, so it must be something I'm doing wrong.
它只识别第一个参数。我在 LinkedIn 和 Twitter 共享上遇到了同样的问题,所以一定是我做错了什么。
回答by Dexter
Rather than html encoding
your URL parameter, you need to URL encode
it:
而不是html encoding
您的 URL 参数,您需要URL encode
它:
http://www.facebook.com/sharer.php?&t=FOOBAR&u=http%3A%2F%2Fwww.foobar.com%2F%3Ffirst%3D12%26sec%3D25%26position%3D
You can do this easily in most languages - in javascript:
您可以在大多数语言中轻松完成此操作 -在 javascript 中:
var encodedParam = encodeURIComponent('www.foobar.com/?first=1&second=12&third=5');
// encodedParam = 'http%3A%2F%2Fwww.foobar.com%2F%3Ffirst%3D12%26sec%3D25%26position%3D'
(there are equivalent methods in other languages too)
(其他语言也有等价的方法)
回答by Sean Vieira
You are missing the ?
in the second URL (Also, it should be URL-encoded to be %3F
).
您?
在第二个 URL中丢失了(此外,它应该是 URL 编码的%3F
)。
Also, I believe that the remaining &
need to be URL, not HTML-encoded. Change &second=12&third=5
to %26second=12%26third=5
and everything should just work.
另外,我认为剩下的&
需要是 URL,而不是 HTML 编码。更改&second=12&third=5
为%26second=12%26third=5
,一切都应该正常工作。
This:
这个:
&u=http://www.foobar.com/first=12&sec=25&position=2
should be:
应该:
&u=http://www.foobar.com/%3Ffirst=12%26sec=25%26position=2
回答by SSL46
In jQuery, you can use:
在 jQuery 中,您可以使用:
let myObject = {first:1, second:12, third:5};
jQuery.param(myObject);
Doc: http://api.jquery.com/jquery.param/The output: first=1&second=12&third=5 This will format it, whatever your object contain.
文档:http: //api.jquery.com/jquery.param/输出:first=1&second=12&third=5 这将格式化它,无论您的对象包含什么。
回答by justkt
In your example parts of your passed-in URL are not URL encoded (for example the colon should be %3A, the forward slashes should be %2F). It looks like you have encoded the parameters to your parameter URL, but not the parameter URL itself. Try encoding it as well. You can use encodeURIComponent
.
在您的示例中,传入 URL 的部分不是 URL 编码的(例如,冒号应为 %3A,正斜杠应为 %2F)。看起来您已将参数编码到您的参数 URL,而不是参数 URL 本身。也尝试对其进行编码。您可以使用encodeURIComponent
.
回答by TheoPlatica
I see you're having issues with the social share links. I had a similar issue at some point and found this question, but I don't see a complete answer for it. I hope my javascript resolution from below will help:
我了解到您在使用社交分享链接时遇到了问题。我在某个时候遇到了类似的问题并发现了这个问题,但我没有看到完整的答案。我希望我下面的 javascript 解析会有所帮助:
I had default sharing links that needed to be modified so that the URL that's being shared will have additional UTM parameters concatenated.
我有需要修改的默认共享链接,以便共享的 URL 将连接其他 UTM 参数。
My example will be for the Facebook social share link, but it works for all the possible social sharing network links:
我的示例将用于 Facebook 社交分享链接,但它适用于所有可能的社交分享网络链接:
The URL that needed to be shared was:
需要分享的网址是:
https://mywebsitesite.com/blog/post-name
The default sharing link looked like:
默认共享链接如下所示:
$facebook_default = "https://www.facebook.com/sharer.php?u=https%3A%2F%2mywebsitesite.com%2Fblog%2Fpost-name%2F&t=hello"
I first DECODED it:
我首先解码它:
console.log( decodeURIComponent($facebook_default) );
=>
https://www.facebook.com/sharer.php?u=https://mywebsitesite.com/blog/post-name/&t=hello
Then I replaced the URL with the encoded new URL (with the UTM parameters concatenated):
然后我用编码的新 URL 替换了 URL(连接了 UTM 参数):
console.log( decodeURIComponent($facebook_default).replace( window.location.href, encodeURIComponent(window.location.href+'?utm_medium=social&utm_source=facebook')) );
=>
https://www.facebook.com/sharer.php?u=https%3A%2F%mywebsitesite.com%2Fblog%2Fpost-name%2F%3Futm_medium%3Dsocial%26utm_source%3Dfacebook&t=2018
That's it!
就是这样!
Complete solution:
完整的解决方案:
$facebook_default = $('a.facebook_default_link').attr('href');
$('a.facebook_default_link').attr( 'href', decodeURIComponent($facebook_default).replace( window.location.href, encodeURIComponent(window.location.href+'?utm_medium=social&utm_source=facebook')) );
$facebook_default = $('a.facebook_default_link').attr('href');
$('a.facebook_default_link').attr('href', decodeURIComponent($facebook_default).replace(window.location.href, encodeURIComponent(window.location.href+'?utm_medium=social&utm_source=facebook')));
回答by jonezy
You have to escape the & character. Turn your
你必须转义 & 字符。转动你的
&
into
进入
&
and you should be good.
你应该很好。