Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is window.location undefined on all properties in Safari 6 and Mobile Safari (iOS 6)

When I type

window.location

I get undefined on all properties.

Here's my console:

enter image description here

like image 210
Chris Abrams Avatar asked Jun 25 '12 22:06

Chris Abrams


People also ask

Does window location work in all browsers?

The window. location is read/write on all compliant browsers.

How do I access Windows location?

Windows location settings give you control over whether Windows features can access your device's location and which Windows apps can use your device's location and location history information. To check your location settings, go to Start > Settings > Privacy > Location.

What is Windows location return?

window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page.


2 Answers

This code works in Safari (6.0.1) and earlier

    // Get location for REST TARGETS

    lcnURI = window.location.protocol + "//" + window.location.hostname + ":"
            + window.location.port + "/rest/";

It appears that window.location "goes out of context" when you look at in the log. But in place used it's fine.

like image 139
jim Schimpf Avatar answered Sep 21 '22 08:09

jim Schimpf


I can confirm this (Version 6.0 (8536.25)). Seems to be a Safari bug, as far as I can tell.

Tested on jsbin (http://jsbin.com/enugoj/1/):

console.log(window.location);
console.log(window.location.pathname);​

Results in Safari's console:

Location
  ancestorOrigins: undefined
  hash: undefined
  host: undefined
  hostname: undefined
  href: undefined
  origin: undefined
  pathname: undefined
  port: undefined
  protocol: undefined
  search: undefined
  __proto__: LocationPrototype

/enugoj/1

Results in Chrome (Version 21.0.1180.89):

Location
  ancestorOrigins: DOMStringList
  assign: function () { [native code] }
  hash: ""
  host: "jsbin.com"
  hostname: "jsbin.com"
  href: "http://jsbin.com/enugoj/1"
  origin: "http://jsbin.com"
  pathname: "/enugoj/1"
  port: ""
  protocol: "http:"
  reload: function () { [native code] }
  replace: function () { [native code] }
  search: ""
  toString: function toString() { [native code] }
  valueOf: function valueOf() { [native code] }
  __proto__: Location
  1:14

/enugoj/1 
like image 35
Davorin Avatar answered Sep 18 '22 08:09

Davorin