[Javascript] Problem w/Javascript, interpolation, functions, onclick

Nathan V. Patwardhan nvp at noopy.org
Tue Oct 24 22:16:31 CDT 2006


All,

I'm populating a series of table cells dynamically, like so:

  var actions = new Array (
      ['Cancel', 'edit_release(this, "cancel")'],
      ['Submit', 'submit_release(this)']
  );

  for(var i=0; i<action_count; i++) {
       var el_type = 'div';
       var action_name = actions[i][0];
       var action_cmd = actions[i][1];

       // Make "button" for action to perform.
       //
       var actions_button = document.createElement(el_type);
       actions_button.id = action_name;
       actions_button.innerHTML = action_name;

       // this should be migrated to a CSS
       actions_button.style.color = 'blue';
       actions_button.style.textDecoration = 'underline';

       try {
           // Works w/Mozilla & Firefox
           var do_it = 'function () { ' + action_cmd + ' }';
           actions_button.onclick = eval(do_it);
       } catch(err) {
           // With MSIE... not so much.
           try {
               // This obviously doesn't work.  Try something else.
               actions_button.onclick = function () { return eval(action_cmd); }
           } catch(err) {
               alert("err 2 " + err);
           }
       }

       ... add to table cell here ...

In the example above, I'm able to assign an anonymous function to a
variable, then eval() this string in an onclick handler.  The code
works under Firefox/Conqueror/Safari.  However, it simply won't work
under IE 6 (and earlier, from what I can tell).  It throws an error
that tells me eval() has failed.

No matter where I put the call to eval() in this code, MSIE fails to work.
At best, MSIE evaluates only the *last* item in action_cmd[].

Arggh!

Anyhow, I've also tried to populate action_cmd[] like so:

  var action_cmd = new Array(
      ['Cancel', edit_release(this, "cancel")],
      ['Submit', submit_release(this)]
  );

And this still fails, but this time around functions are executed, but
exactly how I'd hope.

Is there a workaround for this issue under MSIE?

-- 
Nathan V. Patwardhan
nvp[!at!]noopy.org
aim: notoriousnvp71
"There's nothing worse than Jews with trees." -- Larry David, on his
 wife buying a Christmas tree




More information about the Javascript mailing list