在 Javascript 中将 URL 作为 GET 参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3540184/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:05:01  来源:igfitidea点击:

Passing a URL as a GET parameter in Javascript

javascripturl

提问by MikeD

I am trying to make a bookmarklet that uses the user's current URL, kind of like the tinyURL bookmarklet that uses this javascript code

我正在尝试制作一个使用用户当前 URL 的书签,有点像使用此 javascript 代码的 tinyURL 书签

javascript:void(location.href='http://tinyurl.com/create.php?url='+location.href)

So I copied that same thing and made

所以我复制了同样的东西并制作了

javascript:void(location.href='http://mywebsite.com/create.php?url='+location.href)

Then I use:

然后我使用:

$url=$_GET['url']; 

to retrieve it. The problem is, if I am on a url that already has some get style info in the url, it messes everything up.

取回它。问题是,如果我在一个已经在 url 中有一些 get 样式信息的 url 上,它会把一切搞砸。

Example, If I am on:

例如,如果我在:

http://www.google.ca/webhp?um=1&hl=en&safe=off

The '_GET' code sets $url to be

'_GET' 代码将 $url 设置为

http://www.google.ca/webhp?um=1

So I think the info in google URL is messing up all of my URL parsing, I imagine I am doing something very incorrectly or someone has a very elegant solution for this. What should I do? please help

所以我认为 google URL 中的信息弄乱了我所有的 URL 解析,我想我做的事情非常不正确,或者有人对此有一个非常优雅的解决方案。我该怎么办?请帮忙

回答by Crozin

URLhas a specified format. That part after ?, or to be more exactly between ?and #if exists, is called query string. It contains a list of key-value pairs - a variable name, =character and the value. Variables are separated by &:

URL具有指定的格式。之后的部分?,或者更确切地说是介于?#如果存在,称为查询字符串。它包含一个键值对列表——一个变量名、=字符和值。变量由 分隔&

key1=value1&key2=value2&key3=value3&key4=value4

You should escape location.hrefas it cancontains some special characters like ?, &or #.

您应该转义,location.href因为它可能包含一些特殊字符,例如?,&#

To escape string in JavaScript use encodeURIComponent()function like so:

要在 JavaScript 中转义字符串,请使用如下encodeURIComponent()函数:

location.href = "http://tinyurl.com/create.php?url=" + encodeURIComponent(location.href)

It will replace characters like &into %26. That sequence of characters isn't treated as a variable separator so it will be attached as a variable's value.

它将替换像&into 之类的字符%26。该字符序列不被视为变量分隔符,因此它将作为变量值附加。

回答by Greg

Try

尝试

javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href));

回答by Crayon Violent

so what are you wanting, just the url without the query string?

所以你想要什么,只是没有查询字符串的网址?

$url = explode('?',$_GET['url']);
$url = $url[0];

回答by Alec Gorge

javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href))

You need to escape the characters.

您需要对字符进行转义。

回答by T.Todua

To pass url in $_GET parameter like this:

要像这样在 $_GET 参数中传递 url:

http://yoursite.com/page.php?myurl=http://google.com/bla.php?sdsd=123&dsada=4323

then you need to use encode function:

那么你需要使用编码功能:

echo 'http://yoursite.com/page.php?url='.urlencode($mylink);

//so, your output (url parameter) will get like this
//http://yoursite.com/page.php?url=http%3A%2F%2Fgoogle.com%2Flink.php%3Fname%3Dsta%26car%3Dsaab

after that, you need to decode the parameter with:

之后,您需要使用以下方法解码参数:

$variab = $_GET['url'];
$variab = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\1;",urldecode($variab)); 
$variab = html_entity_decode($variab,null,'UTF-8');
echo $variab;

such way, you can pass the correct link as a parameter.

这样,您可以将正确的链接作为参数传递。