Get Current Url With Jquery

Get current URL with jQuery?

To get the path, you can use:

var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url = window.location.href; // Returns full URL (https://example.com/path/example.html)
var origin = window.location.origin; // Returns base URL (https://example.com)

How to get current url in jquery

You could simply use the location.pathname property to get the part between the first and second slash and run code based on this.

console.log(window.location.pathname);

console.log(window.location.pathname.split("/")[1] === "foo");

How to get URL parameter using jQuery or plain JavaScript?

Best solution here.

var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;

for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');

if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
};

And this is how you can use this function assuming the URL is,

http://dummy.com/?technology=jquery&blog=jquerybyexample.

var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');

Jquery Ajax Get the current URL on submit form

The window.location.href property returns the URL of the current page.

console.log(window.location.href);

using jQuery to get current URL / path name and detect if changes

Are you looking to act upon the change of hash or new url? For hash handling:

$(window).on('hashchange', function(e){
// handle the show/ hide of element here
})

Otherwise, you can use location object to find the URL values liek host, href etc.

$(location).attr('href');

$(location).attr('pathname');

Getting part from current url with jQuery

Do a .split() on &.

let url = $(location).attr("href");
let requiredUrl = url.split('&')[0];

Getting full URL with jQuery including parameters after question mark?

You have:

window.location.href

and also:

document.URL


Related Topics



Leave a reply



Submit