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

2
  • If this will not be dynamic till change-password, you can split it and get the desired string. Commented Jan 23, 2017 at 9:30
  • Possible duplicate of remove url parameters with javascript or jquery Commented Jan 23, 2017 at 9:30

6 Answers 6

1

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

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

Comments

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

Comments

0

use This :

document.location.origin

1 Comment

this will return "localhost:9003" , but op wants "localhost:9093/store"
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.

Comments

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

2 Comments

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.
Hmm I think you are correct but I guess we are dealing with a static url in this context.
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.

Comments

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.