0

Fix all errors in this code?

So I discovered jsLint a few minutes ago. (https://www.jslint.com/) I decided to try it out as i'm trying to improve my coding skills. I pasted a code I wrote that displays errors on screen with the line number that the error occurred on. Here's the code: "use strict"; try { //Empty try block. You can add whatever you want here. } catch(error) { window.onload = function ErrorMessage() { var erroutput = document.getElementById("Error"); var err = error.stack; err = err.match(/:[0-9]*:/); err = error + " Line " + err + ")"; err = err.replace(/:/g, ""); erroutput.innerText = err; }; } How can I correct all of the errors I receive? 1. Unexpected "use strict". (No idea why this is occurring) "use strict"; 2. Empty block. (Easily Fixable) try { 3. Unexpected trailing space. (Easily Fixable) (This is just from the empty try block, which is fixed by adding code to the try block) This function needs a "use strict" pragma. (No idea why this is occurring) window.onload = function ErrorMessage() { EDIT: Turning off "a browser" gives me these errors: 1. Undeclared 'window'. window.onload = function ErrorMessage() { 2. Undeclared 'document'. var erroutput = document.getElementById("Error");

13th Aug 2018, 9:36 PM
Daniel Cooper
Daniel Cooper - avatar
4 Réponses
0
Try this: (function () { "use strict"; try { //Empty try block. You can add whatever you want here. } catch(error) { window.onload = function ErrorMessage() { var erroutput = document.getElementById("Error"); var err = error.stack; err = err.match(/:[0-9]*:/); err = error + " Line " + err + ")"; err = err.replace(/:/g, ""); erroutput.innerText = err; }; } }()); https://code.sololearn.com/Wc84OIX0W0Sm/?ref=app
14th Aug 2018, 1:09 AM
Calviղ
Calviղ - avatar
0
(function () { "use strict"; var alert = alert; try { alert("Hello World!"); } catch (error) { window.onload = function ErrorMessage() { var erroutput = document.getElementById("Error"); var err = error.stack; err = err.match(/:[0-9]*:/); err = error + " (Line " + err + ")"; err = err.replace(/:/g, ""); erroutput.innerText = err; }; } }()); This is the code that had absolutely no errors as long as long as "a browser" is checked.
14th Aug 2018, 1:47 AM
Daniel Cooper
Daniel Cooper - avatar
0
A browser is check for browser JavaScript codes. Uncheck it for Node.js codes. Node.js run from server, does not has DOM objects ljke window or document.
14th Aug 2018, 2:10 AM
Calviղ
Calviղ - avatar
0
Adding a self-executing anonymous function for JavaScript module to prevent setting of "use strict" and variable declarations affect other JavaScript codes which are not belong to the module. function() { // all JavaScript codes here }();
14th Aug 2018, 2:15 AM
Calviղ
Calviղ - avatar