Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "convert" window.location.pathname from object to string?

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/

like image 872
Solo Avatar asked Nov 24 '15 13:11

Solo


People also ask

What is window location pathname?

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.

How do I use Windows location HREF in typescript?

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.

What is window location object?

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.

What is window location href?

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.


1 Answers

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.

like image 144
Charlie Avatar answered Nov 11 '22 02:11

Charlie