Priyanka Taneja
Priyanka Taneja

Reputation: 157

How to fetch url of application?

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

Upvotes: 0

Views: 926

Answers (6)

Himanshu Sahu
Himanshu Sahu

Reputation: 366

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;
    }
}

Upvotes: 0

Shashank
Shashank

Reputation: 2060

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.

Upvotes: 0

bhansa
bhansa

Reputation: 7534

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);

Upvotes: 0

Friedemann Lee
Friedemann Lee

Reputation: 19

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.

Upvotes: 0

fjc
fjc

Reputation: 5845

var url = window.location.origin + (window.location.pathname.indexOf("/")>=0 ? "/" + window.location.pathname.split("/")[1] : "");
alert(url);

Upvotes: 1

Anil Samal
Anil Samal

Reputation: 1053

use This :

document.location.origin

Upvotes: 0

Related Questions