Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#8784 closed bug (wontfix)

globalEval dont allow to catch exceptions

Reported by: sebastien@… 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.

Change History (5)

comment:1 Changed 9 years ago by sebastien@…

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);
    }
}

comment:2 Changed 9 years ago by Rick Waldron

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/

comment:3 Changed 9 years ago by sebastien@…

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

comment:4 Changed 9 years ago by Rick Waldron

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.

comment:5 Changed 9 years ago by anonymous

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.

Note: See TracTickets for help on using tickets.