开发者

location.href in javascript?

开发者 https://www.devze.com 2023-01-02 04:23 出处:网络
javascript: location.href(\"somefile.php\"); // successfully working with IE location.href = \"somefile.php\";

javascript:

  1. location.href("somefile.php"); // successfully working with IE
  2. location.href = "somefile.php";

Ques 1. first code is not work with Safari. Why?

Ques 2. What is the difference b/w th开发者_运维技巧ese codes.

Thanks,


  1. href is not a method, it is a property whose value is a string.
  2. The first is a call on a method with a url as the argument (incorrect), the second is assigning the url as the value of the property (correct).

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


i never heard of location.href("somefile.php");... location.href = "somefile.php"; is the "normal" way that you should use.


Also it's more efficient using window.location than location. So try to use :

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

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


Answer 1 :- It wont work because href is a property of location object, not a method.
Answer 2 :- location.href("...") denotes a method(that is invalid) while location.href is a property.

0

精彩评论

暂无评论...
验证码 换一张
取 消