David542
David542

Reputation: 110267

Redirecting a user to a page that opens a modal

I am using the following approach when I need to redirect a user to a different page and open up a modal on that page:

var qsData = parseQueryString();
var showModal = ("show-modal" in qsData);
if (showModal) {

    // Load the modal and change the url back to the base url
    var modalToDisplay = qsData['show-modal'];
    $(`a[data-modal-body-id="${modalToDisplay}"]`).click();
    window.history.pushState({}, null, window.location.href.split('?')[0]);

}

This way I can pass a url such as:

http://myste.net/account/?show-modal=change-password

And it will open up the "change password" modal for them. There are two things I want to accomplish in doing this redirect:

What would be the best way to accomplish this? That is, changing the url but without storing that in the history? I was thinking of using a session variable instead, but I don't think that would work, as sometimes the url links will come in an email, and so I sometimes won't have a 'state' to start with.

Upvotes: 0

Views: 80

Answers (1)

CodeThing
CodeThing

Reputation: 2768

You should try using

window.history.replaceState({}, null, window.location.href.split('?')[0]);

This will not add new state into the stack. It will replace the current one.

Upvotes: 1

Related Questions