"Script error" reported in window.onerror without cross-site scripting
Last Updated :
03 Apr, 2023
Sometimes, when developing a JavaScript application, you might encounter an issue where a "Script error" is reported in the window.onerror method, even though there is no cross-site scripting (XSS) involved. This can be frustrating because it can be difficult to determine the cause of the error and how to fix it.
The problem you may encounter: Here is an example of how this problem might manifest in your code:
JavaScript
window.onerror = function (message, source, line, col, error) {
console.log(message); // "Script error."
console.log(source); // "http://localhost:8080/scripts.js"
console.log(line); // 0
console.log(col); // 0
console.log(error); // undefined
};
function foo() {
var bar = 1;
console.log(baz); // ReferenceError: baz is not defined
}
foo();
Output(Error): When you will run this code, you will see the following output in the console:
Script error.
http://localhost:8080/scripts.js
0
0
undefined
Solution: To solve this issue, you can try the following approach:
- Check your code for any syntax errors or undefined variables that might be causing the error. If you are loading external scripts, make sure they are hosted on the same domain as your main page to avoid any cross-site scripting issues.
- If you are using a content security policy (CSP), make sure it allows for the execution of inline scripts or the use of eval().
Use a try-catch block to catch any errors and provide a more meaningful error message to the user.
Here is an example of how you could modify the previous code to catch the error and provide a more helpful message:
JavaScript
window.onerror = function (message, source, line, col, error) {
console.log(message);
console.log(source);
console.log(line);
console.log(col);
console.log(error);
};
function foo() {
try {
var bar = 1;
console.log(baz); // ReferenceError: baz is not defined
} catch (error) {
console.error("Error: " + error.message);
}
}
foo();
Output: When you run this modified code, you will see the following output in the console:
Error: baz is not defined
This provides a more helpful error message that tells you exactly what went wrong, making it easier to fix the issue.
Similar Reads
Cross Site Scripting (XSS) Prevention Techniques XSS or Cross-Site Scripting is a web application vulnerability that allows an attacker to inject vulnerable JavaScript content into a website. An attacker exploits this by injecting on websites that doesn't or poorly sanitizes user-controlled content. By injecting vulnerable content a user can perfo
5 min read
How to Fix "R Code Not Recognizing Error (tryCatch) When Webscraping"? Web scraping is a powerful technique for extracting data from websites, but it comes with its own set of challenges, particularly when it comes to handling errors. In R, the tryCatch function is commonly used to manage errors and warnings during web scraping. However, sometimes tryCatch may not beha
4 min read
How to catch an "ReferenceError" with "onerror" ? The "ReferenceError" is a very common error that can occur when programming in JavaScript. The "onerror" event is a great way to catch these errors and prevent them from causing your program to crash.What is a "ReferenceError"?A "ReferenceError" occurs when your program tries to access a variable or
2 min read
How to get rid of JavaScript error prompt ? Javascript is the most popular programming language that comes installed on every modern browser like Google Chrome, Mozilla Firefox, Internet Explorer, Microsoft Edge, etc. Browsers respond differently to JavaScript depending upon its settings or user settings.In this article, we will learn about h
3 min read
Using multiple script tags to prevent errors In this article, we will see how can we use multiple <script> tags inside the HTML code to prevent from getting errors. To understand this concept, we will first see what kind of error we usually get on the console and then how that can be removed from the code by adding multiple script tags.
3 min read