Good points TJ.
I was thinking about something along these lines:
(Note that all features are tested when function is declared - the
actual Prototype.exec only needs to go through few "if" blocks)

(function() {

  var globalEvalSupported = false;
  window.eval('var __INCLUDE_TEST_1__ == true;');
  if (!Object.isUndefined(__INCLUDE_TEST_1__)) {
    globalEvalSupported = true;
    delete window.__INCLUDE_TEST_1__;
  }

  var execScriptSupported = !!window.execScript;

  var insertScriptSupported = false;
  var insertScript = function(code) {
    var script = new Element('script', {
      type: 'text/javascript',
      text: code
    });
    ($$('head')[0] || document.body).insert(script);
    script.remove();
    return true;
  }
  insertScript('var __INCLUDE_TEST_2__ = true;');
  if (!Object.isUndefined(__INCLUDE_TEST_2__)) {
    insertScriptSupported = true;
    delete window.__INCLUDE_TEST_2__;
  }

  var setTimeoutSuported = false;
  window.setTimeout('var __INCLUDE_TEST_3__ = true;', 0);
  if (!Object.isUndefined(__INCLUDE_TEST_3__)) {
    delete window.__INCLUDE_TEST_3__;
    setTimeoutSupported = true;
  }

  Prototype.exec = function(code) {
    if (!Object.isString(code)) return false;

    if (windowEvalSupported) {
      return window.eval(code);
    }
    if (execScriptSupported) {
      return window.execScript(code);
    }
    if (insertScriptSupported) {
      return insertScript(code);
    }
    if (setTimeoutSupported) {
      return window.setTimeout(code, 0);
    }
    return false;
  }
})()

- kangax

On Mar 24, 7:36 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> It's an interesting implementation; would need some cleanup, IMHO.
> The things I see off-hand are:
>
> 1. window.__INCLUDE_TEST_2__ is never deleted if it's successfully
> created.
>
> 2. _insertScriptTag is defined on "this" and then left there, but not
> reused if it exists.  I don't see that it needs to be defined on
> "this" as opposed to referencing it with a local variable, unless it's
> going to be retained and reused -- in which case I wouldn't create it
> within the function anyawy.
>
> 3. Would be useful if the return value were false in the case where
> none of the methods worked (even though you haven't found any engines
> where it doesn't yet).
> --
> T.J. Crowder
> tj / crowder software / com
>
> On Mar 24, 3:14 am, kangax <[EMAIL PROTECTED]> wrote:
>
> > I actually really like the solution in one of the comments on Dean's
> > "sandbox.eval" post [1]. It does no browser sniffing and degrades
> > nicely to "script insertion" on browsers which don't support
> > eval.call(window, ... ) and execScript. The "check" for eval can be
> > done when prototye loads - this should not affect performance - and
> > produce a boolean indicating whether eval works or not. I'm still
> > confused whether we should sacrifice code size for "no-browser-
> > sniffing". What do you think?
>
> > [1]http://dean.edwards.name/weblog/2006/11/sandbox#comment70076
>
> > - kangax
>
> > On Mar 23, 10:30 am, John-David Dalton <[EMAIL PROTECTED]>
> > wrote:
>
> > > I don't have an issue with Prototype.exec(),
> > > I choose exec rather than Prototype.eval (because issues with browsers
> > > that reserve the word eval for only the real eval method only), it is
> > > shorter than Prototype.globalEval(), its not negatively associated
> > > with eval() <evil>, and  eval isn't used once in the method.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to