views:

191

answers:

4

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.

A: 

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.

meder
I tried this as well. Works great in Fx, no go in IE. I could see my change to the DOM in the debugger, but apparently it wasn't visible to the flash object, because the behavior was exactly as the param was coded in the HTML.
TomG
Can you clarify the questions I just added?
meder
I did code it with the "default" version of the param in the html, and modified the "value" attribute via the DOM.I was using "variant 1" as a shortcut since the actual param is about 60 characters long.
TomG
The actual html was closer to <param id='playerVars' name='settings' value='some-string'>and the javascript was like this: if (document.getElementById p.value = p.value + playmode; }(playmode is set based on the querystring)
TomG
A: 

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

Anthony
I'll give that a try, but it's pretty close to what I had. I found that the onload was too late, as apparently the object had already been instantiated (even in Firefox), so I moved the DOM code to just after the <object> definition, which fixed it in Fx, but not IE8.
TomG
A: 

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.

Ben
+1  A: 

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