Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get escaped URL parameter

I'm looking for a jQuery plugin that can get URL parameters, and support this search string without outputting the JavaScript error: "malformed URI sequence". If there isn't a jQuery plugin that supports this, I need to know how to modify it to support this.

?search=%E6%F8%E5 

The value of the URL parameter, when decoded, should be:

æøå 

(the characters are Norwegian).

I don't have access to the server, so I can't modify anything on it.

like image 200
Sindre Sorhus Avatar asked Sep 10 '09 07:09

Sindre Sorhus


People also ask

How do you escape a parameter in a URL?

URL escape codes for characters that must be escaped lists the characters that must be escaped in URLs. If you must escape a character in a string literal, you must use the dollar sign ($) instead of percent (%); for example, use query=title%20EQ%20"$3CMy title$3E" instead of query=title%20EQ%20'%3CMy title%3E' .

How do you get the colon out of the URL?

action=parse&page=:title includes a parameter named "title"). If you want to include a colon as it is within a REST query URL, escape it by url encoding it into %3A .

How do you escape characters in URI?

If you are parsing a URI component, you should unescape any percent-encoded sequence (this is safe, as '%' characters are not allowed to appear bare in a URI). If you are “cleaning up” a URI that someone has given you: Escape the following characters: space and “<>\^`{|} and U+0000 — U+001F and U+007F and greater.

Do I need to encode query parameters?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing them into a URL.


2 Answers

function getURLParameter(name) {     return decodeURI(         (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]     ); } 
like image 143
James Avatar answered Oct 19 '22 13:10

James


Below is what I have created from the comments here, as well as fixing bugs not mentioned (such as actually returning null, and not 'null'):

function getURLParameter(name) {     return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; } 
like image 29
radicand Avatar answered Oct 19 '22 13:10

radicand