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(loca
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.