开发者

Changing <object> paramaters at page load in IE8

I have a plain html web page with an embedded flash object. 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. I replaced the entire script with a simple alert("a") call, and that was not executed by IE8, either. 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?

I've replaced the whole with document.write calls, and that works fine, but I'd prefer something closer to my original attempt.


I think the problem lies in the DOM and with Internet Explorer seemingly not executing in-line 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.

The following worked as expected in both Firefox and 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? Also, are you sure you don't want variant="1" instead of "variant 1"? Don't you need a name attribute somewhere?

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


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

  2. I changed the params to be more XHTML valid, using <param /> instead of <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 . 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜