EN
Il contenuto non è disponibile nella lingua di destinazione, ma solo in inglese.

Attribute Differences in Windows Internet Explorer 8

Note  As of December 2011, this topic has been archived and is no longer actively maintained. For more information, see Archived Content. For information, recommendations, and guidance regarding the current version of Windows Internet Explorer, see Internet Explorer Developer Center.

Starting with Windows Internet Explorer 8, there is a distinction between the value of an attribute as it is specified by a page author and the representation of the value in the Document Object Model (DOM). This distinction can affect the functionality of Web sites originally designed to support older versions of the browser. This article explains the distinction between content attributes and DOM attributes DOM, demonstrates how this distinction impacts Web pages, and shows how to obtain each value using JavaScript.

Because DOM object properties frequently share the same name as corresponding HTML attributes, it is common to think of the two values as being the same. For example, the background attribute of the body object seems to be the same value as the background attribute of the body element in a Web page. In previous versions of Internet Explorer, the value reported by the property was the same value assigned to the element attribute. While the value of the property is based on the value of the element attribute, each value is actually separate and distinct. Traditionally, the terminology between these values has been ambiguous. The HTML5 (Working Draft) standard clarifies the terminology for each value and Internet Explorer 8 incorporates this terminology and distinguishes between these values.

When you refer to the attribute of an element in a Web page, a content attribute value is reported. Content attributes typically contain the values originally specified by the page author.

When you refer to the property of a DOM object, the DOM attribute value is reported; As you might expect, DOM attributes generally report values as they are represented in the DOM.

As of Internet Explorer 8, DOM attribute values may differ from content attribute values for pages displayed in IE8 Standards mode. The reasons for these differences are varied and may include security, standards compliance, and compatibility with other browsers. Pages displayed in older document compatiblity modes report the values for both DOM attributes and content attributes.

To illustrate, consider the following sample Web page. Notice that the body element uses a background attribute to specify a background image. The page also contains a button to display the name of the image file assigned to the background attribute.


<html>
<head>
   <title>Content/DOM Attribute Example</title>
   <meta http-equiv="X-UA-Compatible" content="IE=8"/>
   <base href="http://contoso.com/content/">
   <script>

   function showInfo() {
      var obj = document.getElementById( "body1");
      var sAttrValue = obj.getAttribute( "background");
      
      // Displays the Content Attribute 
      // value ("../images/background.gif")
      alert( sAttrValue );
   }
   </script>
</head>

<body id="body1" background="../images/background.gif">
   <p>This page has a background image. Click the button to see the value of the background attribute.</p>
   <button onclick="showInfo()">Background image</button>
</body>
</html>


The getAttribute and getAttributeNode methods return content attributes. As a result, the alert box displays the relative file path specified by the original author of the Web page.

The following sample is similar to the previous one; however, it reports the DOM attribute value because the value is taken from the background property of an object variable.


<html>
<head>
   <title>Content/DOM Attribute Example</title>
   <meta http-equiv="X-UA-Compatible" content="IE=8"/>
   <base href="http://contoso.com/content/">
   <script>

   function showInfo() {
      var obj = document.getElementById( "body1");
      var sAttrValue = obj.background;
      
      // Displays the DOM attribute value
      // ("http://contoso.com/images/background.gif")
      alert( sAttrValue );
   }
   </script>
</head>

<body id="body1" background="../images/background.gif">
   <p>This page has a background image. Click the button to see the value of the background attribute.</p>
   <button onclick="showInfo()">Background image</button>
</body>
</html>


Note  In most cases, DOM attributes share the same name as their corresponding content attributes. However, because DOM attributes can be accessed by a variety of programming languages, there are exceptions. For example, className is the DOM attribute for the class content attribute and htmlFor is the DOM attribute for the for content attribute).

While each of the previous samples essentially do the same thing; that is, they report the value of the background attribute, the actual value depends on the way the value is obtained. As a result, the distinction between content attributes and DOM attributes can affect the behavior of Web pages that rely on the values originally specified by the original page author.

The distinction between DOM attributes and content attributes applies only to pages displayed in IE8 mode. When pages are viewed in other document compatibility modes or older versions of the browser, DOM attributes report the same values as their corresponding content attributes. If you experience unexpected behavior, verify the document compatibility mode of your web page. For more information, see Defining Document Compatibility.

Related topics

Defining Document Compatibility
Scripting with Elements and Collections

 

 

Mostra:
© 2014 Microsoft