Nilesh
answered Apr 27 '23 00:00
window.location. href and window. open () are both methods in JavaScript that are used to manipulate the browser's window and URL.
window.location. href is a property that returns the current URL of the page as a string. You can also set this property to a new URL to redirect the browser to a different page. For example, to redirect the browser to a new page, you can use:
window.location.href = "https://www.example.com";
This will redirect the browser to the URL "https://www.example.com".
window.open() is a method that opens a new browser window or tab. You can specify the URL of the page to open as the first parameter, and various options such as window size, position, and features as the second parameter. For example, to open a new window with the URL "https://www.example.com", you can use:
window.open("https://www.example.com", "_blank", "width=500,height=500");
This will open a new window with the specified dimensions, and the URL "https://www.example.com " will be loaded in the new window.
One important thing to keep in mind is that some browsers may block window.open( ) calls that are not initiated by user actions, such as button clicks or other user interactions. This is done to prevent unwanted pop-ups and other potentially harmful behavior.
In summary, window.location.href is used to change the URL of the current page, while window.open() is used to open a new window or tab with a specified URL and options.