Techie Hints

A place where to find tech solution

How to replace url with only specific param change in javascript ?

This method reloads the page,

I am adding the code which is url independent, this following code uses pageNo param from html code.

In the url for current page will be added with param page-no  if not present else it will replace the value for page-no param value in quey param.

Please find the useful code below for above usage

function reloadPage(){

pageNo = $(“#pageNo”).val();

var curUrl = window.location.href;
var re = /page-no=[ ]*(\w)*&{0,1}/g;

var matchedpattern = curUrl.match(re);

if( matchedpattern == null ) {
url = curUrl.split(“?”);
curUrl = url[0] + “?page-no=”+pageNo+(url[1] != ‘undefined’ ? ‘&’+url[1]: ”);

} else {
curUrl = curUrl.replace(matchedpattern,”page-no=”+pageNo+”&”);
}

window.location.href = curUrl;

}

 

Thanks for referring my blog.  Leave your query or any suggestion in the comment box. Still you can leave your feedback & suggestion to admin@techiehints.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: