3

I have to add either an embed tag for Firefox or an object tag for Internet Explorer with JavaScript to address the appropriate ActiveX / Plugin depending on the browser. The plugin could be missing and needs to get downloaded in this case. The dynamically added embed tag for Firefox works as expected. The dynamically added object tag for Internet Explorer seems to do nothing at all. The object tag needs the following attributes to function properly.

id ="SomeId" classid = "CLSID:{GUID}" codebase = "http://www.MyActicexSource.com/MyCuteActivex.CAB#Version=2,0,0,1"

Even a general working idea or method would be nice.

Thanks!


I needed to do this same thing and simply place all of the HTML needed for the OBJECT tag in a string in JavaScript and simply replace the innerHTML of a div tag with the OBJECT HTML and it works in IE just fine.

// something akin to this:
document.getElementById(myDivId).innerHTML = "<OBJECT id='foo' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'.....etc";

That should work, it does just fine for me - I use it to embed Windows Media Player in a page.


UPDATE: You would run the above code after the page loads via an event handler that either runs on the page's load event or maybe in response to a user's click. The only thing you need to do is have an empty DIV tag or some other type of tag that would allow us to inject the HTML code via that element's innerHTML property.


UPDATE: Apparently you need more help than I thought you needed? Maybe this will help:

Have your BODY tag look like this: <body onload="loadAppropriatePlugin()">

Have somewhere in your page, where you want this thing to load, an empty DIV tag with an id attribute of something like "Foo" or whatever.

Have code like this in a <script> tag in your <head> section:

function getIEVersion() { // or something like this
   var ua = window.navigator.userAgent;
   var msie = ua.indexOf("MSIE ");
   return ((msie > 0) ? parseInt(ua.substring(msie+5, ua.indexOf(".", msie))) : 0);
}

function loadAppropriatePlugin() {
    if(getIEVersion() != 0) { // this means we are in IE
        document.getElementById("Foo").innerHTML = "<OBJECT id='foo' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'.....etc";
    } else {
        // if you want to maybe do the same for FF and load that stuff...
    }
}

Does that help?

4

3 回答 3

13

我需要做同样的事情,只需将 OBJECT 标签所需的所有 HTML 放在 JavaScript 中的字符串中,然后将 div 标签的 innerHTML 替换为 OBJECT HTML,它就可以在 IE 中正常工作。

// something akin to this:
document.getElementById(myDivId).innerHTML = "<OBJECT id='foo' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'.....etc";

这应该可以,它对我来说很好 - 我用它来在页面中嵌入 Windows Media Player。


更新:您将在页面加载后通过事件处理程序运行上述代码,该事件处理程序在页面的加载事件上运行,或者可能响应用户的点击。您唯一需要做的就是有一个空的 DIV 标记或其他类型的标记,它们允许我们通过该元素的innerHTML属性注入 HTML 代码。


更新:显然你需要比我想象的更多的帮助?也许这会有所帮助:

让您的 BODY 标签看起来像这样:<body onload="loadAppropriatePlugin()">

在您希望加载此内容的页面中的某个位置有一个空的 DIV 标记,其id属性类似于“Foo”或其他内容。

<script>在您的部分的标签中有这样的代码<head>

function getIEVersion() { // or something like this
   var ua = window.navigator.userAgent;
   var msie = ua.indexOf("MSIE ");
   return ((msie > 0) ? parseInt(ua.substring(msie+5, ua.indexOf(".", msie))) : 0);
}

function loadAppropriatePlugin() {
    if(getIEVersion() != 0) { // this means we are in IE
        document.getElementById("Foo").innerHTML = "<OBJECT id='foo' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'.....etc";
    } else {
        // if you want to maybe do the same for FF and load that stuff...
    }
}

这有帮助吗?

于 2008-11-11T15:55:04.957 回答
1
var object = document.createelement('object')
object.setAttribute('id','name')
object.setAttribute('clssid','CLSID:{}')

And the same for other parameters.

于 2010-05-03T15:50:44.540 回答
-5

Two ways.

1) Just do a document.write where ever you want it

<script type="text/javascript">
<!--
   document.write("<object id=\"SomeId\" classid=\"CLSID:{GUID}\" codebase=\"http://www.MyActicexSource.com/MyCuteActivex.CAB#Version=2,0,0,1\"></object>");
-->
</script>

2) Edit a tag's innerHTML property.

<div id="my-div"></div>
<script type="text/javascript">
<!--
   document.getElementById("my-div").innerHTML = "<object id=\"SomeId\" classid=\"CLSID:{GUID}\" codebase=\"http://www.MyActicexSource.com/MyCuteActivex.CAB#Version=2,0,0,1\"></object>";
-->
</script>

EDIT: Just a note, it is best to not use JavaScript to do this, since people with JavaScript enabled will never see the object. It would be better to just place it in your HTML.

于 2008-11-11T15:57:48.477 回答