Skip to content Skip to sidebar Skip to footer

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 javascript:void(location.href='http://tinyur

Solution 1:

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 &:

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

You should escape location.href as it can contains some special characters like ?, & or #.

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

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.

Solution 2:

Try

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

Solution 3:

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

You need to escape the characters.

Solution 4:

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

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

Solution 5:

To pass url in $_GET parameter like this:

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.

Post a Comment for "Passing A Url As A Get Parameter In Javascript"