How to prevent Javascript redirect in window.location


In Javascript, you can change location by using some methods like

location="newlocation"
location.href="newlocation"

However, in some cases, you want to prevent it. How to do it?

Example: you are in a webpage with URL (https://mail.google.com/mail/u/0/#inbox)

F12 and go to tab Console to run this command:

location='http://google.com'

You will redirect to website: http://google.com

But, if you freeze object location, you can prevent redirect

Object.freeze(location)

Now you can’t change location by using below methods.

Object.freeze

Leave a Reply