简体   繁体   English

改变中 <object> IE8中页面加载时的参数

[英]Changing <object> paramaters at page load in IE8

I have a plain html web page with an embedded flash object. 我有一个带有嵌入式Flash对象的纯HTML网页。 Based on presence of a query string, I want to vary one of the paramters to the object, so I wrote something like this: 基于查询字符串的存在,我想更改对象的参数之一,因此我写了这样的内容:

<object  ...>
   <param ...>
   <param ...>
   <script type="text/javascript">
      if (window.location.search.indexOf("stuff")>=0) {
          document.write("<param  variant 1>");
      } else {
          document.write("<param variant 2>");
      }
   </script>
</object>

It works great in Firefox, but IE8 does not execute the js code, at all. 它在Firefox中运行良好,但是IE8完全不执行js代码。 I replaced the entire script with a simple alert("a") call, and that was not executed by IE8, either. 我用一个简单的alert(“ a”)调用替换了整个脚本,而IE8也未执行该调用。 Using the developer tool, I can see the script, but it's not highlighted, nor can I set a breakpoint in it, so I'm pretty sure it's not acknowledging it as a valid script. 使用开发人员工具,我可以看到该脚本,但是该脚本未突出显示,也无法在其中设置断点,因此,我很确定它没有将其确认为有效脚本。

Is this an IE restriction, or am I missing something? 这是IE限制,还是我缺少什么?

I've replaced the whole with document.write calls, and that works fine, but I'd prefer something closer to my original attempt. 我已经用document.write调用代替了整个方法,并且效果很好,但是我希望更接近我最初的尝试。

I think the problem lies in the DOM and with Internet Explorer seemingly not executing in-line JavaScipt. 我认为问题出在DOM上,Internet Explorer似乎没有执行嵌入式JavaScipt。

Waiting for the window to load before initialising, and then adding the parameters as children (rather than writing HTML) is the proper way to go, it seems. 看来,在初始化之前先等待窗口加载,然后将参数添加为子代(而不是编写HTML)是正确的方法。

The following worked as expected in both Firefox and Internet Explorer. 以下各项在Firefox和Internet Explorer中均按预期工作。

<html>
<head>
    <script type="text/javascript">

        listen(window, "load", "onload", initiate);

        // called when the window finishes loading
        function initiate() {
            checkStuff();
        }

        function checkStuff() {

            var myObj = document.getElementById("myObj");
            var elm = document.createElement('param');

            if (window.location.search.indexOf("stuff") >= 0){
                elm.setAttribute("name", "param1");
                elm.setAttribute("value", "true");
            } else {
                elm.setAttribute("name", "param2");
                elm.setAttribute("value", "true");
            }

            myObj.appendChild(elm);
        }

        // just a simple function to attach events to objects
        function listen(obj, event_normal, event_alt, func) {
            if (obj.attachEvent) {
                obj.attachEvent(event_alt, func);
            } else if (obj.addEventListener) {
                obj.addEventListener(event_normal, func, false);
            } else {
                obj.addEventListener(event_normal, func, false);
            }
        }

    </script>
</head>
<body>
    <object id="myObj"></object>
</body>
</html>

Does it fail if you try it the proper DOM way? 如果以正确的DOM方式尝试它会失败吗? Also, are you sure you don't want variant="1" instead of "variant 1" ? 另外,您确定不希望variant="1"而不是"variant 1"吗? Don't you need a name attribute somewhere? 您在某处不需要name属性吗?

<object id="foo"></object> 
<script>
if (window.location.search.indexOf("stuff")>=0) {
    var object = document.getElementById('foo'), param = document.createElement('param');
    param.setAttribute('variant', '1'); // not sure if you want variant=1 or variant=variant AND 1=1
    object.appendChild( param );
} else { /* add the rest */ }
</script>

I stripped out some of the html. 我删除了一些html。

I wrote the following based on your example: 我根据您的示例编写了以下内容:

   <script type="text/javascript">
    window.onload = function() {
      if (window.location.search.indexOf("stuff")>=0) {
          alert("stuff!");
      } else {
          alert("nope!");
      }
     };
   </script>

   <object>
       <param />
       <param />
   </object>

And it works fine in IE8. 并且它在IE8中可以正常工作。 I made two fundamnetal changes: 我做了两个基本的改动:

  1. I moved the script out of the HTML and made it run as a function tied to the onload event. 我将脚本移出了HTML,并使其作为与onload事件相关的函数运行。

  2. I changed the params to be more XHTML valid, using <param /> instead of <param> . 我改变了params更有效XHTML,使用<param />代替<param>

This might not be the answer you're looking for, but when it comes to doing anything dynamic with Flash, I just give up and use SWFObject . 这可能不是您要找的答案,但是当涉及到对Flash进行动态处理时,我只是放弃并使用SWFObject There is so much complicated and non-standard stuff going on when rendering Flash, and it varies across browser, Flash version, etc. So relying on an external library might be less elegant than finding the most efficient and minimalistic solution for embedding in a given situation, but saves a lot of time and you're less likely to find out later that it's broken in browser X. 呈现Flash时,发生了太多复杂且非标准的事情,并且在不同的浏览器,Flash版本等中也有所不同。因此,依赖外部库可能不如找到最有效,最简单的嵌入到给定解决方案的方法优雅情况,但节省了大量时间,以后您不太可能在浏览器X中发现它已损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM