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
Group Array of Object Nesting Some of the Keys With Specific Names
Find an Element in Dom Based on an Attribute Value
How to Update the Parent'S State in React
I'Ve Heard Global Variables Are Bad, What Alternative Solution Should I Use
Screen Styling When Virtual Keyboard Is Active
Mobile Safari on iOS Crashes on Big Pages
JavaScript Console.Log() in an iOS Uiwebview
How to Perform SQL "Like" Operation on Firebase
Use of .Apply() With 'New' Operator. Is This Possible
How to Avoid 'Cannot Read Property of Undefined' Errors
Converting a Js Object to an Array Using Jquery
Event.Preventdefault() Function Not Working in Ie
How to Show a Running Progress Bar While Page Is Loading
Jquery Event That Triggers After CSS Is Loaded
Hide Scrollbar and Show on Hover Like Facebook's New Chat Sidebar
Performance Impact of Using CSS/JavaScript Source-Maps in Production