How to get URL’s & Querystrings using jQuery
URL = http://www.domain.com/post.aspx?postid=39&posttype=General
1. Get the current URL in jQuery
var currentURL = document.URL;
Result : http://www.domain.com/post.aspx?postid=39&posttype=General
------------------------------------------------------------------
var currentURL = window.location.href;
Result : http://www.domain.com/post.aspx?postid=39&posttype=General
------------------------------------------------------------------
2.Get the pathname
var currentURL = location.pathname;
Result : post.aspx?postid=39&posttype=General
------------------------------------------------------------------
3.Get the hostname
var currentURL = location.hostname;
Result : www.domain.com
------------------------------------------------------------------
4.Get A URL Hash Parameter
var currentURL = location.hash;
Result : null
------------------------------------------------------------------
5.Read Querystring Parameters
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var postid = GetQueryStringParams('postId');
Result : 39
var posttype= GetQueryStringParams('posttype');
Result : General
Or
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}
var postid = GetParameterValues('postId');
Result : 39
var posttype= GetParameterValues('posttype');
Result : General
------------------------------------------------------------------
1. Get the current URL in jQuery
var currentURL = document.URL;
Result : http://www.domain.com/post.aspx?postid=39&posttype=General
------------------------------------------------------------------
var currentURL = window.location.href;
Result : http://www.domain.com/post.aspx?postid=39&posttype=General
------------------------------------------------------------------
2.Get the pathname
var currentURL = location.pathname;
Result : post.aspx?postid=39&posttype=General
------------------------------------------------------------------
3.Get the hostname
var currentURL = location.hostname;
Result : www.domain.com
------------------------------------------------------------------
4.Get A URL Hash Parameter
var currentURL = location.hash;
Result : null
------------------------------------------------------------------
5.Read Querystring Parameters
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var postid = GetQueryStringParams('postId');
Result : 39
var posttype= GetQueryStringParams('posttype');
Result : General
Or
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}
var postid = GetParameterValues('postId');
Result : 39
var posttype= GetParameterValues('posttype');
Result : General
------------------------------------------------------------------
URL = http://www.gigasters.com/post.aspx?postid=39
URL = http://www.gigasters.com/post.aspx?postid=39
URL = http://www.gigasters.com/post.aspx?postid=39
Comments
Post a Comment