Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Debug eval sources

Unsere Freiwilligen haben diesen Artikel noch nicht in Deutsch übersetzt. Machen Sie mit und helfen Sie, das zu erledigen!

You can debug JavaScript code that is evaluated dynamically, either as a string passed to eval() or as a string passed to the Function constructor.

To do this, you need to use the //# sourceURL directive to name the source:

var button = document.getElementById("clickme");
button.addEventListener("click", evalFoo, false);

var script = "function foo() {" +
             "  console.log('called foo');" +
             "}" +
             "foo();//# sourceURL=my-foo.js";

function evalFoo() {
  eval(script);
}

This names the script to "my-foo.js".

Once the string has been evaluated it will appear in the Debugger as a separate source, and will be fully debuggable like any other source. You'll also be able to pretty-print it:

Its name will also appear in stack traces appearing in the Web Console.

The debugger will also stop at debugger; statements in unnamed eval sources.

Schlagwörter des Dokuments und Mitwirkende

 Zuletzt aktualisiert von: wbamberg,