Location.href in javascript?

JavaScript:

  • location.href ("somefile.php"); // successful work with IE
  • location.href = "somefile.php";

Ques 1. The first code does not work with Safari. Why?
Ques 2. What is the difference between these codes.

Thank,

+3
source share
4 answers
  • href not a method, this is a property whose value is a string.
  • Firstly, it is a method call with the URL as an argument (incorrect), the second assigns url as the value of the property (correct).

See also: http://www.w3.org/TR/Window/#location

+13
source

location.href("somefile.php");... location.href = "somefile.php"; "" , .

+1

It is also more efficient using window.location than location. So try using:

window.location.href = "somefile.php";

(as Andy said, href is a property, and in JS you define the value of a property this way: object.property = "value")

+1
source

Answer 1: - This will not work because href is a property of a location object, not a method.
Answer 2: - location.href ("...") denotes a method (which is invalid), and location.href is a property.

0
source

Source: https://habr.com/ru/post/1748661/


All Articles