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
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
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
You need to escape the characters.
To pass url in $_GET parameter like this:
then you need to use encode function:
after that, you need to decode the parameter with:
such way, you can pass the correct link as a parameter.
Try
so what are you wanting, just the url without the query string?
URL has 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&
:You should escape
location.href
as it can contains some special characters like?
,&
or#
.To escape string in JavaScript use
encodeURIComponent()
function like so: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.