Get URL parameter in javascript

Sometimes in javascript you have the variable that you need to use in the url, as a parameter passed in…

November 25, 2010

Sometimes in javascript you have the variable that you need to use in the url, as a parameter passed in GET to the page, but you don’t have it in page. You can retrieve those data analyzing the window.location.href string. Here is the Javascript function that does the work, pass to it the variable name of the variable you need:

function gup( varname ) {
	varname = varname.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
	var regexS = "[\?&]"+varname+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) return ""; else return results[1];
}

Another usefull function is the one that get the file name of the current page from the url.

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Get URL parameter in javascript”

2 thoughts

Comments are closed

Recommended

Get file name in javascript

How to get the file name of the page? When you need to read the file name of the current…

How to change twitter status with php and curl without oAuth

Twitter api authentication Since the 31 of august 2010, twitter made its API more secure, stopping basic authentication calls. So,…

September 9, 2010

PHP bot to get wikipedia definitions

Wikipedia, the collaborative and multilingual encyclopedia project, has a lot of usefull terms defined in its database, you can find…

August 29, 2010

PHP to post on a Facebook page

Hi, I’ve modified the Mini Bot Class, I’ve fixed the Facebook status update and I’ve implemented the function to post…

July 28, 2010

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache