Reputation: 1443
I have the following html code:
<!DOCTYPE html>
<html>
<head>
<title>
Test window.Element
</title>
</head>
<body>
<script>
alert(window.Element);
</script>
</body>
</html>
The page is hosted in IIS on a remote machine. If I access the page using the host name like this http://testserver/index.html, then the alert tells me that window.Element is undefined. If I access the page like this http://testserver.com/index.html (that is, I included a dot in the hostname), then window.Element is in fact a valid object.
So, my first question is, why does IE 11 (not sure about other versions) do such a thing?
The reason I need window.Element is because I am working with some legacy code that tries to setup eventListeners for old versions of ie like this:
!window.addEventListener && Element.prototype && function (a) {
/* setup event listeners an old-fashioned way */
}
Since Element is undefined when the hostname doesn't have a dot, this code blows up. I would also like to know if there is a better way to do this check for old versions of IE that don't support addEventListener?
Upvotes: 1
Views: 237
Reputation: 3285
Load each page and then:
I think you'll find that the pages are loaded in different security zones, depending on the dot. Because of the different zones, you'll get different document modes and see behavioral differences similar to the ones you describe.
Host names without dots are interpreted as network references and generally load in the Intranet zone. This zone is generally used by businesses and enterprises to host older applications, so IE assumes you're more interested in legacy compatibility than interoperability. If my suspicion is correct, your dot-free URL is loading in IE7 standards mode. IE7 didn't support addEventListener (supported was added in IE9).
By default, host names with dots are loaded in the Internet zone, which assumes you want standards interoperability and standards compliance, so your page is loaded in Edge mode. Edge mode supports addEventListener, so your later code works.
For older versions of IE that don't support addEventListener, use attachEvent. The registerEvent sample function shown in this article is a reasonably fair start. The overall concept is called document compatibility and it can get confusing very quickly.
The original idea was to allow web devs the opportunity to choose the focus that best served their needs. Unfortunately, the implementation was so flexible, it became confusing.
Hope this helps...
-- Lance
P.S. If you need to load an Intranet page in a different security zone, e.g. the Internet zone, you can use a mark of the web to do so.
Upvotes: 2