如何动态添加< object>在IE中用JavaScript标记? [英] How can I dynamically add an <object> tag with JavaScript in IE?

查看:167
本文介绍了如何动态添加< object>在IE中用JavaScript标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为Firefox添加embed标记或使用JavaScript添加Internet Explorer的对象标记,以根据浏览器寻址相应的ActiveX /插件。插件可能丢失,需要在这种情况下下载。动态添加的Firefox嵌入标记按预期工作。 Internet Explorer的动态添加对象标记似乎什么都不做。 object标签需要以下属性才能正常运行。

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.

谢谢!

推荐答案

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

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";

这应该有用,它对我来说没问题 - 我用它来嵌入Windows Media Player页面。

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

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

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:

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

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

在您的页面中的某处,您希望加载此内容,一个带有 id的空DIV标记像Foo之类的东西的属性。

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.

< script> 中有这样的代码您的< head> 部分中的标记:

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...
    }
}

这有帮助吗?

这篇关于如何动态添加&lt; object&gt;在IE中用JavaScript标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆