[英]Passing a URL as a GET parameter in Javascript
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. 问题是,如果我在网址上已经有一些获取样式信息的网址,它会混淆一切。
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 请帮忙
URL has a specified format. URL具有指定的格式。 That part after ?
之后的那部分?
, or to be more exactly between ?
,还是更确切地介于?
之间?
and #
if exists, is called query string . 和#
exists,称为查询字符串 。 It contains a list of key-value pairs - a variable name, =
character and the value. 它包含一个键值对列表 - 变量名称, =
字符和值。 Variables are separated by &
: 变量由&
分隔:
key1=value1&key2=value2&key3=value3&key4=value4
You should escape location.href
as it can contains some special characters like ?
您应该转义location.href
因为它可以包含一些特殊字符,如?
, &
or #
. , &
或#
。
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
. 它将替换%26
&
%26
类的字符。 That sequence of characters isn't treated as a variable separator so it will be attached as a variable's value. 该字符序列不被视为变量分隔符,因此它将作为变量的值附加。
尝试
javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href));
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. 这样,您可以将正确的链接作为参数传递。
javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href))
你需要逃避角色。
so what are you wanting, just the url without the query string? 那么你想要什么,只是没有查询字符串的网址?
$url = explode('?',$_GET['url']);
$url = $url[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.