IE8 Javascript document.domain error

2019-08-13 03:17发布

问题:

I need to change the domain of the document and then set it back again to the original value.

It looks something like this [the page is on domain base.site.com]:

function execute ()
{
document.domain = "site.com";

// Access an object that is on another frame, but did the same set of the domain

document.domain = "base.site.com";

// Access an object that is on this page (window.createPopup ())
}

The problem is that this works fine on IE6 (I did not test on 7). But it gives me an error [Invalid argument] when i execute the second document.domain.

Is there any way to "reset" the domain of the document in IE8?

回答1:

The problem is you cannot set document.domain to a more strict value once you set it to top-domain only.

From the MSDN blog:

Put simply, once you’ve loosened document.domain, you cannot tighten it.

http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx



回答2:

Use script tag with type text/cjs:

<script type="text/cjs">
document.domain = '<your domain host value>';
</script>