Skip to main content

Bug Tracker

Side navigation

#8784 closed bug (wontfix)

Opened April 06, 2011 08:06AM UTC

Closed April 07, 2011 02:07PM UTC

Last modified April 08, 2011 07:45AM UTC

globalEval dont allow to catch exceptions

Reported by: sebastien@cheminel.com Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:
Description

When doing

try{
   $.globalEval(code);
}catch(e){
   alert(e.message);
}

on a syntaxicly uncorrect code, the error is not catched.

Attachments (0)
Change History (5)

Changed April 06, 2011 08:17AM UTC by sebastien@cheminel.com comment:1

I take a look à $.globalEval source code.

I dont know why you choose this architecture. And as a newbee in this tracker I don't know where to look (I hope I will) for a discussion/specification about this.

But I can say, by now, that this (innocent) function works for me:

var tools= {
    globalEval : window.execScript ? function(src) {
      window.execScript(src);
    } : function() {
      window.eval.call(window, src);
    }
}

Changed April 07, 2011 02:07PM UTC by rwaldron comment:2

component: unfiledcore
priority: undecidedlow
resolution: → wontfix
status: newclosed

globalEval is not an "eval" in the traditional sense as it creates a script tag and injects the code to execute into that script tag. The only way to reliably catch exceptions would be to wrap the code to execute in a try/catch before injecting it into the script tag.

http://jsfiddle.net/rwaldron/H84u3/

Changed April 07, 2011 03:12PM UTC by sebastien@cheminel.com comment:3

globalEval is not "scriptInsert" so it should admit try catch.

I think you're not answering this ticket. Closing it is rude. ;)

Your example is not a "syntaxically uncorrect code".

The wrap method is not suitable in a "syntaxically uncorrect code" case.

I which I knew where to list all discutions about this subject.

I've looked in jquery forums but didn't find any spécifications.

However I found a lot off entries and I understand a little why this implementation was chosen.

I'll search for a technical answer but I almost hopped a little help about where is the documentation.

Maybe the right answer is look at all forums entries.

I'm ready to spend time trying to implement a catchable solution.

Thx

Note : syntaxicly == syntaxically

Changed April 07, 2011 05:07PM UTC by rwaldron comment:4

Ready for this? Read the source before telling someone they are wrong, or you're going to look silly:

Taken from L564 src/core.js:


// Evalulates a script in a global context
globalEval: function( data ) {
  if ( data && rnotwhite.test(data) ) {
    // Inspired by code by Andrea Giammarchi
    // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
    var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
      script = document.createElement( "script" );

    if ( jQuery.support.scriptEval() ) {
      script.appendChild( document.createTextNode( data ) );
    } else {
      script.text = data;
    }

    // Use insertBefore instead of appendChild to circumvent an IE6 bug.
    // This arises when a base node is used (#2709).
    head.insertBefore( script, head.firstChild );
    head.removeChild( script );
  }
},

Where you see: script.appendChild( document.createTextNode( data ) ); or script.text = data;... both demonstrating plain text being injected into a newly created script tag.

Changed April 08, 2011 07:45AM UTC by anonymous comment:5

Thank you for your answer and for your time.

Sorry to bother you but you're talking about the way it's implemented not what it should do.

I've read the code before opening the ticket and I maintain that :

globalEval is not scriptInsert.

" script inserting " is just a workarround.

If you read the code you should see it.

By the way, I just want to be sure that all discussion and documentation about this subject is in this forum to (try to). I'd really like to make my own implementation and to push it to JQuery.

No matter if this ticket is closed.

My opinion is :

  • globalEval is not eval neither scriptInserting
  • globalEval is made to "Evalulates a script in a global context"
  • the scriptInsert workarround is a very good one and should not be changed with no good reason.
  • globalEval should let try/catch syntax exception. Wich it doesn't by now.

I just want to make a try by implementing my own solution. But I wanted to be sure that everything that have been discussed will be tested correctly.

I you don't help me this way I will start with forum and versionning informations.

Peacefully,

Sébastien.