如何在IE中动态添加带有JavaScript的标签?

时间:2022-06-15 15:06:40

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.

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

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

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!

3 个解决方案

#1


13  

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.

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

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

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


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.

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


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()">

让你的BODY标签看起来像这样:

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.

在你的页面中的某个地方,你想要加载这个东西,一个空的DIV标签,其id属性类似于“Foo”或其他什么。

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?

这有帮助吗?

#2


1  

var object = document.createelement('object')
object.setAttribute('id','name')
object.setAttribute('clssid','CLSID:{}')

And the same for other parameters.

其他参数也一样。

#3


-4  

Two ways.

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

1)只需要在任何你想要的地方做一个document.write

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

2)编辑标签的innerHTML属性。

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

编辑:只是一个注释,最好不要使用JavaScript来执行此操作,因为启用JavaScript的人永远不会看到该对象。将它放在HTML中会更好。

#1


13  

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.

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

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

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


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.

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


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()">

让你的BODY标签看起来像这样:

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.

在你的页面中的某个地方,你想要加载这个东西,一个空的DIV标签,其id属性类似于“Foo”或其他什么。

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?

这有帮助吗?

#2


1  

var object = document.createelement('object')
object.setAttribute('id','name')
object.setAttribute('clssid','CLSID:{}')

And the same for other parameters.

其他参数也一样。

#3


-4  

Two ways.

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

1)只需要在任何你想要的地方做一个document.write

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

2)编辑标签的innerHTML属性。

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

编辑:只是一个注释,最好不要使用JavaScript来执行此操作,因为启用JavaScript的人永远不会看到该对象。将它放在HTML中会更好。