go
Expand Minimize

doImport method

Dynamically imports an element behavior.

Syntax

var retval = namespace.doImport(bstrImplementationUrl);

Parameters

bstrImplementationUrl [in]

Type: String

String that specifies the URL of the element behavior to import into the namespace.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Standards information

There are no standards that apply here.

Remarks

Note  doImport is disabled for Internet Explorer 10 and later.

Once imported into a namespace by this method, an element behavior can be used on the document where the namespace has been declared.

Using this method alone is not enough to enable an element behavior to function on a document; it is also necessary to add a custom element to the body of the primary document, as illustrated in the following code snippet.


<body>
<myns:mycustomtag/>
</body>

The preceding sample shows how a custom element makes use of a custom namespace, which is declared on the XMLNS attribute of the HTML tag.

A script or binary behavior can also be used to insert a custom element. See the examples that follow for an illustration of this technique.

Examples

The following example declares a namespace called "TESTNS" and imports the default Internet Explorer behaviors into it.


<html XMLNS:TESTNS>
<head>
  <script>
    document.namespaces("TESTNS").doImport("#default");
  </script>
</head>

The following example shows how the doImport method can be used in conjunction with the createElement to insert a custom element to which an element behavior is attached.


<html xmlns:myns>
<body onload="Load()">
  <script>
    var ns; // holds the newly created namespace object
    function Load() {
      ns = document.namespaces[0];
      ns.doImport("redbg.htc");

      if (ns.readyState != "complete") {
        // Wait for the element behaviors to finish downloading
        ns.attachEvent("onreadystatechange", addTagnamesToBody);
      }
      else {
        addTagnamesToBody();
      }
      return true;
    }
    function addTagnamesToBody() {
      if (ns.readyState != "complete") return;
      var v = document.createElement("myns:abc");
      v.innerText = "ElementBehavior";
      document.body.appendChild(v);
      ns.detachEvent("onreadystatechange", addTagnamesToBody);
    }
  </script>
</body>
</html>

The following code shows the content of the HTC file, redbg.htc, which simply applies a red background when the ondocumentready event fires.


<public:component tagName=abc >
<public:attach event=ondocumentready onevent=Doc_Ready() />
</public:component>
<script> 
  function Doc_Ready() {
	   element.document.bgColor = "red";
  }
</script>

See also

namespace
Reference
IMPORT
Conceptual
Introduction to Viewlink
About Element Behaviors

 

 

Show:
© 2014 Microsoft