I'm trying to split the following url:
http://www.store.com/products.aspx/Books/The-happy-donkey
in order to get only http://www.store.com/products.aspx
I'm using JavaScript window.location.href
and split
but not success so far.
How can this be done? thanks!
var newURL="http://www.example.com/index.html/homePage/aboutus/"; console. log(newURL); var splitURL=newURL. toString(). split("/"); console.
What are the parts of a URL? A URL consists of five parts: the scheme, subdomain, top-level domain, second-level domain, and subdirectory.
Method #1 : Using split() This is one of the way in which we can solve this problem. We split by '? ' and return the first part of split for result.
Try this
var fullurl = "http://www.store.com/products.aspx/Books/The-happy-donkey",
url = fullurl.split(".aspx")[0] + ".aspx";
In the case of a url: http://www.store.com/products.aspx/Books/The-happy-donkey from the address bar
var path = window.location.pathname;
var str = path.split("/");
var url = document.location.protocol + "//" + document.location.hostname + "/" + str[1];
This isn't unwieldy, is it?
var url = 'http://www.store.com/products.aspx/Books/The-happy-donkey';
[
url.split('://')[0] + '://',
url.split('://')[1].split('/').slice(0,2).join('/')
].join('')
A little less cheeky:
url.split('/').slice(0, 4).join('/')
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