iPad orientation info gathered from a child object

2019-07-19 17:43发布

问题:

I am working on a fullscreen Ad, served through a publisher site that I do not have direct access to. The Ad utilises the tilt information from the iPad to change colours on the Ad. Everything works well standalone as it's own web page, but when served through our Ad engine, the html appears as a child window Object, and the 'deviceorientation' does not update in the Ad.

I'm quite surprised by this, because the tilt info does get through to the Ad, but when I try to find the parent window orientation, i get the js cross domiain warnings.

Is there anything I can do to get the device orientation to be passed to the child window? Or any sensible workaround

Thanks

回答1:

The answers depends on how the domains are structured.

They have same root domain

Then yes. If your parent window is hosted on let's say "company.com" and your child window is hosted on "hifi.company.com" (or vice versa) then by setting the below property for both windows, they will be able to communicate.

document.domain = 'company.com';

You will no longer get cross-site scripting violations when you do this

parent.onorientationchange = function () {
     console.log(parent.orientation);
};

They have different root domains

Then sadly no. You will not be able to access orientation information of the parent window by the child window. The closest you can get is listening to the resize event of the child window itself. But this depends on how the iframe is sized as well as how the viewport is configured.