Themeforest

Get URL Parameters Using jQuery

Todays post is a very simple, yet surprisingly useful piece of code, which will get the parameter after the ‘#’ symbol in the web page URL.

Being able to extract the URL parameter with jQuery is obviously very useful and allows us to then use this value in our jQuery functions.

In our demo jQuery code we are going to take the URL parameter, insert this value into a text block and display on the screen.

The jQuery Code

$(document).ready(function() {

    //  Get the parameter value after the # symbol
    var url=document.URL.split('#')[1];
	if(url == undefined){
		url = '';
	}

    // If the parameter exists create the message and insert into our paragraph
    if(url != ''){
        var message = 'You clicked '+url;
        $('#parameter').text(message);
    }
});

And that’s all there is! Try out the demo to see it in action:

View jQuery Demo

Leave a comment

To add code to your comments wrap the code text in [text][/text] tags