5

I'm experiencing rather weird behavior when trying to log a window object defined by $window.open() in AngularJS within an $interval

self = this
$scope.childWindow = $window.open(authService.buildAuthorizeUrl(), '_blank')
console.log $scope.childWindow
var1 = "I may not work"
self.var2 = 'I should work'
privateData.authInterval = $interval ->
  console.log $scope.childWindow
  console.log var1
  console.log self.var2
,
  1000

Output

Window {document: document, window: Window, frameElement: null, clientInformation: Navigator, onhashchange: null…}
Window {}
I may not work
I should work
Window {}
I may not work
I should work 

As you can see, the first console.log $scope.childWindow is outputting a fully defined window object. All others, inside the $interval, are outputting only {}. I've tried not attaching childWindow to the $scope object, and I've tried attaching it to self. I've also tried following this example too and experienced the same behavior. Anyone have any idea why this is happening? Thanks much.

JSFiddle demo: http://jsfiddle.net/U3pVM/15124/

Alex_Faber
  • 117
  • 1
  • 8

2 Answers2

1

I tried your code in a browser and while setting a debug point inside the $interval function an empty object is getting logged in the console but the watch inspector on the right shows that $scope.childWindow is not empty. So you might just be able to use $scope.childWindow.

enter image description here

  • I tried inspecting it as well, and found a similar result. My end goal is to grab the location of the `childWindow` by using `$scope.childWindow.window.location.href`, but that doesn't seem to be happening. Location is defined, but I can't tell what its properties are. [These](http://www.w3schools.com/jsref/obj_location.asp) are the properties apparently, but they're not available either. Weird stuff. – Alex_Faber May 01 '15 at 19:42
0

I learned this shouldn't work in the browser because of security reasons, which may explain why it's getting "overwritten." The use case of this was actually in a Blackberry10 Cordova application. To fix this, you must disable web security in config.xml like so:

<access origin="*"/>
<preference name="websecurity" value="disable" />

Of course the * will whitelist all domains. To tighten security down a bit, add domains you'd like to whitelist.

Alex_Faber
  • 117
  • 1
  • 8