Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me with the difference between window.location.href and window.open() methods in JavaScript?

3 Answers

0 votes
by (26.7k points)

Basically, window.location.href is not a method, it is used to provide the current URL location of your browser. Also, by changing its property it can redirect the page. Whereas, window.open() is a method that is used to open the URL in a new window. For example:

window.location.href:

window.location.href = 'http://www.google.com'; //Will take you to Google.

window.open():

window.open('http://www.google.com'); //This will open Google in a new window.

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Tutorial:

0 votes
by (37.3k points)
  1. window. location.href

This window.location.href is basically helps to get or set the URL you are currently working on. If you click on some URL or link it will navigate to that particular URL.

Example:

// Redirect the current page to a new URL

window.location.href = 'https://www.example.com';

  1. window.open()

This is used to open a new window or the tab with a specified URL.

And also we can add additional parameters such as its  size, toolbar 

And scrollbars.

Example:

// Open a new window with a specified URL and features

window.open('https://www.example.com', '_blank', 'width=800,height=600');

0 votes
by (2.8k points)

window.location.href is not a method, it's a property that will tell you the present URL location of the browser. 

window.open() is a method where you can pass a URL in that when you want to open it in a new window.

For example:

window.location.href example:

window.location.href = 'http://www.flipkart.com'; //Will take you to Flipkart.

window.open() example:

window.open('http://www.flipkart.com'); //This will open Flipkart in a new window.

Related questions

0 votes
1 answer
asked Feb 16, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Mar 31, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...