I need pathname (www.my-site.com/this-part/and-this-part/etc/) in JS/jQuery but I need it as string not as object.
In other words I need $_SERVER['REQUEST_URI'];
in JS/jQuery.
I've tried:
var page_pathname = location.pathname;
var page_pathname = location.pathname + location.search;
var page_pathname = (location.pathname+location.search).substr(1);
All I get with console.log
:
1. Object {error: Object}
2. Location {hash: "", search: "", pathname: "/my-cat/my-title/", port: "", hostname: "www.my-site.com"…}
What I need with console.log
: my-cat/my-title/
window.location.pathname returns the path and filename of the current page. window.location.protocol returns the web protocol used (http: or https:) window.location.assign() loads a new document.
href = 'https://google.com'; }); If you load the page and click on the button, you will navigate to the webpage you assigned to the property. The location. href property returns a string containing the whole URL and allows the href to be updated.
The Window Location Object The location object contains information about the current URL. The location object is a property of the window object. The location object is accessed with: window.location or just location.
window. location. href is not a method, it's a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.
window.location.pathname
is already a string.
You can also try:
String(window.location.pathname)
.
This is explicit conversion to string.
window.location.href
will also help you in retrieving the full url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With