9

What is the difference between window.location.href () and window.open() methods in JavaScript?

Community
  • 1
  • 1
khalid jarrah
  • 1,561
  • 5
  • 14
  • 18
  • Possible duplicate of [Window.location.href and Window.open () methods in JavaScript](https://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript) – Ethan Oct 02 '17 at 12:21

2 Answers2

22

window.location is an Object and

window.location.href is its property

It tells you the current URL location of the browser

document.write(location.href);// will give location URL location of browser.

Setting the property will redirect the page.

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

E.g

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

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

Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
5

window.location.href changes the immediate window location.

window.open() will open a new window.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Lloyd
  • 29,197
  • 4
  • 84
  • 98