-2

My Url looks like: http://google.com/stackoverflow/post/1

Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:

Post . How to do it ? Thanks !

user3571494
  • 167
  • 1
  • 8
  • In what language? There are many ways to do it. You can look for the 4th slash, or for "overflow/" and get a substring of everything up to there. Or you can use regular expressions. Look up those functions (google 'string functions' and 'php' or 'javascript') and try to figure it out. – Hamza Kubba Apr 25 '14 at 04:51
  • possible duplicate of [Get the full URL in PHP](http://stackoverflow.com/questions/6768793/get-the-full-url-in-php) -- this, plus a little basic string manipulation. – pennstatephil Apr 25 '14 at 04:51

3 Answers3

2

Plz try this:

var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];

Thanks.

Raja Danish
  • 235
  • 1
  • 7
1

use location Object

location.href
location.pathname
Vikram Jakkampudi
  • 502
  • 1
  • 3
  • 16
1

UPDATED:

Using JavaScript:

var url = "http://google.com/stackoverflow/post/1";    // or var url = window.location;

var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);

if( matches ){
    var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
    alert( newURL );
}

document.getElementById('post_link').href = newURL;

HTML:

<a id="post_link">post</a>

See JSFiddle

Kyo
  • 974
  • 5
  • 10