Test the compatibility of IE browser with JavaScript AngularJS
This article mainly introduces how to test the compatibility of IE browser with AngularJS in JavaScript. Although with the recent release of Windows 10, IE browser will soon become a history... For more information, see
Short version
To ensure that Angular applications work on IE, confirm the following:
1. polyfill JSON. stringify in IE7 or earlier versions. You can use JSON2 or JSON3 for polyfills.
?
| 1 2 3 4 5 6 7 8 9 10 11 |
<! Doctype html> <Html xmlns: ng = "http://angularjs.org"> <Head> <! -- [If lte IE 7]> <Script src = "/path/to/json2.js"> </script> <! [Endif] --> </Head> <Body> ... </Body> </Html> |
2. Add id = "ng-app" to the root element at the connection, and use the ng-app attribute
?
| 1 2 3 4 |
<! Doctype html> <Html xmlns: ng = "http://angularjs.org" id = "ng-app" ng-app = "optionalModuleName"> ... </Html> |
3. You cannot use custom element tags, such as <ng: view> (use Attribute version <div ng-view> instead), or
4. If you must use custom element tags, you must take the following steps to ensure that IE8 and earlier versions can be used:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<! Doctype html> <Html xmlns: ng = "http://angularjs.org" id = "ng-app" ng-app = "optionalModuleName"> <Head> <! -- [If lte IE 8]> <Script> Document. createElement ('ng-include '); Document. createElement ('ng-pluralize '); Document. createElement ('ng-view '); // Optionally these for CSS Document. createElement ('ng: include '); Document. createElement ('ng: pluralize '); Document. createElement ('ng: view '); </Script> <! [Endif] --> </Head> <Body> ... </Body> </Html> |
5. Replace style = "{someCss}" with the ng-style flag }}". Later versions can work in Chrome and Firefox, but cannot work in IE version <= 11 (the latest version when writing this article ).
Important part:
- Xmlns: ng-namespace-you need to specify a namespace for each custom tag.
- Document. createElement (yourTagName)-create a custom tag name-because this is only a problem with the earlier version of IE, You need to specify the loading conditions. For each tag that does not have a namespace and is not defined in HTML, You need to declare it in advance so that IE can recognize it.
Version Information
IE has many problems with non-standard label elements. These problems can be categorized into two categories, each of which has its own solution.
- If the tag name starts with "my:", it is treated as an XML namespace and must have a corresponding namespace declaration.
- If the tag does not have a symbol but is not a standard HTML tag, you must use document. createElement ('My-tag') to create it in advance.
- If you plan to use the CSS selector to change the style of the custom tag, you must use document. createElement ('My-tag') to create a custom tag no matter whether there is a namespace.
Good news
The good news is that these restrictions only apply to element tag names and not to element attribute names. Therefore, no special processing is required in IE: <div my-tag your: tag> </div>
What will happen if I don't do this?
Assume that you use the unknown HTML tag mytag (the result of my: tag or my-tag is the same ):
?
| 1 2 3 4 5 6 |
<Html> <Body> <Mytag> some text </mytag> </Body> </Html> |
The following DOM should be parsed:
?
| 1 2 3 4 5 |
# Document +-HTML +-BODY +-Mytag +-# Text: some text |
The expected behavior is that the BODY element has a mytag sub-element with some text.
But this is not true in IE (if the above revision is not included)
?
| 1 2 3 4 5 6 |
# Document +-HTML +-BODY +-Mytag +-# Text: some text +-/Mytag |
In IE, the BODY element has three child elements:
1. A self-disabled mytag. For example, self-closing labels
. /Is optional,
Tags cannot have child elements.
Some text
As a tag of three peers, some text is not
.
2. some text is a text node. This should be a child element of mytag, not a peer tag.
3. A corrupted self-disabled/mytag. This is a corrupt element because the element name cannot contain/characters. In addition, the seed closed element is not part of the DOM. It is only used to describe the DOM structure.
CSS-style custom tag name
To ensure that the CSS selector can work on custom elements, the names of custom elements must be created using document. createElement ('My-tag') in advance without worrying about the XML namespace.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<Html xmlns: ng = "needed for ng: namespace"> <Head> <! -- [If lte IE 8]> <Script> // Check whether ng-include is properly parsed. Document. createElement ('ng-include '); // CSS reference must be enabled Document. createElement ('ng: view '); </Script> <! [Endif] --> <Style> Ng \: view { Display: block; Border: 1px solid red; } Ng-include { Display: block; Border: 1px solid blue; } </Style> </Head> <Body> <Ng: view> </ng: view> <Ng-include> </ng-include> ... </Body> </Html> |