Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should use location.assign rather then location.replace #4550

Open
tiberiuichim opened this issue Mar 16, 2023 · 11 comments · May be fixed by #4561
Open

Should use location.assign rather then location.replace #4550

tiberiuichim opened this issue Mar 16, 2023 · 11 comments · May be fixed by #4561

Comments

@tiberiuichim
Copy link
Contributor

window.location.replace(

https://stackoverflow.com/questions/4505798/difference-between-window-location-assign-and-window-location-replace

@saadkhan011
Copy link

Hi tiberiuichim,
I want to work on this issue can you assign me this issue so I can start.
Thanks

@Krishpanchal
Copy link

Hi tiberiuichim, I can work on this issue. You can assign this issue to me.
Thanks

@Darkcoder011
Copy link

i think You Don't Do that Because
replace :- will replace the current document and replace the current History with that URL
assign:- will just cause a new document to load

@tiberiuichim

@aadarsh-nagrath
Copy link
Member

Generally location.assign is typically used to navigate to a new URL, while location.replace is used to replace the current URL with a new one.

If the goal is to allow the user to navigate to a new page while preserving the current page in their browser history, then location.assign may be more appropriate. If the goal is to replace the current page in the browser history with a new one, then location.replace may be more appropriate i guess

@tiberiuichim
Copy link
Contributor Author

honestly, I don't understand the difference. I'm the one that wrote the original code, window.replace().

What is the impact for the browser history? What's the practical difference? As far as we can say, window.replace worked fine till now.

@Darkcoder011
Copy link

the difference is assign is used to navigate new url but the difference replace the current URL with a new one

@tiberiuichim
Copy link
Contributor Author

@Darkcoder011 but from a practical point of view, what happens?

Because even with window.replace, the back button works in the browser. So, it it a case that the last entry from the "list of urls I've visited" is removed and replaced with the new one? And if I use assign, that doesn't happen?

@Darkcoder011
Copy link

@tiberiuichim yes both redirect to new page .
they both do the same job but in difference manner and both have a difference

when we use assign they can load a new page

but when we use replace they directly replace that page

you can see below
for assign:-
document.location.href = 'http://frontbackend.com'; // go to homepage
document.location.assign('http://frontbackend.com/Frontend'); // loads a new page

for replace :-
document.location.href = 'http://frontbackend.com'; // go to homepage
document.location.replace('http://frontbackend.com/Frontend'); // replace to another webpage

@Darkcoder011
Copy link

and in practical point of view they both do same job but in difference manner they both have their own methods
@tiberiuichim

@stevepiercy
Copy link
Collaborator

If I understand this correctly, it depends on whether you want the user to be able to use the back button or use history to go back to the immediately preceding document. Both back button and history would still work for all other locations that do not use replace().

For login/logout, I would not want the user to go back. I would use location.replace(). I might want to use this when adding, editing, or deleting content as well, and possibly throughout the entire authenticated and authorized session.

Where I don't care or I want the user to use the back button or history, I would use location.assign().

@djay
Copy link
Member

djay commented May 24, 2023

@tiberiuichim I suspect this is the code responsible for updating the browser url when p.a.redirector happens on a restapi. in this case you probably don't want the the user to go back to the incorrect url old url so replace would make most sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants