0

Hi i have a change password page after reseting the password it should redirect to the home page. Thia is my URL for change password page.

https://localhost:9003/store/change-password?code=0a05a6d6-2298-4b62-9687-7deae15951e4

i got this using this in javascript.

var url = window.location.href;

I want only this much URL "https://localhost:9003/store" how can i get it?

Regards, Priyanka

Priyanka Taneja
  • 157
  • 2
  • 3
  • 13
  • If this will not be dynamic till change-password, you can split it and get the desired string. – Shubham Jan 23 '17 at 09:30
  • Possible duplicate of [remove url parameters with javascript or jquery](http://stackoverflow.com/questions/4651990/remove-url-parameters-with-javascript-or-jquery) – Amit Kumar Jan 23 '17 at 09:30

6 Answers6

1

var url = window.location.origin + (window.location.pathname.indexOf("/")>=0 ? "/" + window.location.pathname.split("/")[1] : "");
alert(url);
fjc
  • 5,590
  • 17
  • 36
0

use This :

document.location.origin
Anil Samal
  • 1,343
  • 1
  • 13
  • 30
0

it will be done by location's api easily.

    var host = location.origin;
    var path = location.pathname;
    var firstPath = path.split("/")[0];
    var url = host + "/" + firstPath;

the url is what you want.Also you can get the first pathname via Rexg.

0

var url = "https://localhost:9003/store/change-password?code=0a05a6d6-2298-4b62-9687-7deae15951e4";

var u = url.substr(0,url.indexOf('/',23));

alert(u); 

console.log(u);
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • It works as long as the hostname is localhost. But when it is significantly longer or shorter, the fixed 23 offset is not safe to use anymore. – fjc Jan 23 '17 at 09:38
  • Hmm I think you are correct but I guess we are dealing with a static url in this context. – bhansa Jan 23 '17 at 09:43
0

Use the below syntax:

var anchorTag = document.createElement('a');
anchorTag.href = "https://localhost:9003/store/change-password?code=0a05a6d6-2298-4b62-9687-7deae15951e4";
anchorTag.text = "Link";
var hrefText = anchorTag.href;
var url_1 = anchorTag.origin;
var url_2 = hrefText.substr(anchorTag.origin.length, hrefText.indexOf('/'));
var url = url_1 + url_2;
console.log('url = ', url);

Refer to the demo.

Shashank
  • 2,010
  • 2
  • 18
  • 38
0

There is no direct way. but this method will work for you.

function() {
    var href = window.location.pathname;
    var regex = "/[A-Za-z0-9\s]+";
    var strArray = href.match(regex);
    if (strArray && strArray.length) {
        return window.location.protocol + "//" + window.location.hostname + strArray[0];
    } else {
        return null;
    }
}
Himanshu Sahu
  • 366
  • 3
  • 11