46

I have constructed a url path that are pointing to different hostname www.mysite.com, so for example:

var myMainSite = 'www.mymainsite.com' + '/somepath';

so this is equivalent to www.mymainsite.com/path/path/needthispath/somepath.

How I'm doing it now is like the code below and this gives me a bunch of indexes of the url in the console.log.

var splitUrl = myMainSite.split('/');

console.log looks like:

0: http://
1: www.
2: mysite.com
3: path
4: path
5: needthispath
6: somepath

and I concat them like splitUrl[5]+'/'+splitUrl[6] and it doesn't look pretty at all.

So my question is how to split/remove url location http://www.mymainsite.com/ to get the url path needthispath/somepath in js? Is there a quicker and cleaner way of doing this?

6
  • 2
    Can you try that : document.location.pathname Commented Sep 5, 2016 at 16:21
  • @AyyoubDahhane this would only work if the current url is the URL he is aiming to split. The I have constructed a url path that are pointing to _different_ hostname makes me believe otherwise Commented Sep 5, 2016 at 16:21
  • @N.J.Dawson Yeah you are correct. Commented Sep 5, 2016 at 16:22
  • @nCore what is it that you didn't like with my answer that you didn't accept it? was it not explaining well? Commented Sep 6, 2016 at 19:05
  • @inanc oh I did click the tick not sure why it didn't show up. Ah nvm could be because I like the other one too so had to wait for few seconds. Commented Sep 6, 2016 at 20:09

3 Answers 3

87

First solution (URL object)

The URL object can be used for parsing, constructing, normalizing, encoding URLs, and so on.

var url = 'http://www.mymainsite.com/somepath/path2/path3/path4';

var pathname = new URL(url).pathname;

console.log(pathname);

The URL interface represents an object providing static methods used for creating object URLs.


Second solution (a kind of a hack)

var urlHack = document.createElement('a');
urlHack.href = 'http://www.mymainsite.com/somepath/path2/path3/path4';

console.log(urlHack.pathname);

// you can even call this object with these properties:
// protocol, host, hostname, port, pathname, hash, search, origin

Sign up to request clarification or add additional context in comments.

Comments

14

Why don't you use the split function and work from there. The split function will break your URL out fully and from there you just need to look for the second last and last items.

Here is an example:

var initial_url = 'http://www.mymainsite.com/path/path/needthispath/somepath';
var url = initial_url .split( '/' );

var updated_url= document.location.hostname + '/' + url[ url.length - 2 ] + '/' + url[ url.length - 1 ];

1 Comment

that's what I'm using now using the .split function. But I found a way now. :) thanks for the answer.
1

function url($url) {
  var url = $url.split( '//' );
  if (url[0] === "http:" || url[0] === "https:") {
    var protocol = url[0] + "//";
    var host = url[1].split( '/' )[0];
    url = protocol + host;
    var path = $url.split(url)[1];
    return {
      protocol: protocol,
      host: host,
      path: path
    };
  }
}

var $url = url("http://www.mymainsite.com/path/path/needthispath/somepath");

console.log($url.protocol); // http://
console.log($url.host); // www.mymainsite.com
console.log($url.path); // /path/path/needthispath/somepath

1 Comment

Good answers include some explanation, not just code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.