Web Performance Monitoring

Script Error: What is it and how to solve it

By: Anthony Simon|Last Updated: Mar 28, 2023

A generic "Script Error" is logged in the browser console when an uncaught JavaScript error occurs in a file served from a different domain or subdomain.

When this happens, the browser will not provide all the details of the error for security and privacy reasons. As a result, the error message can be vague and unhelpful, making it difficult to understand what is causing the issue.

Why the Script Error occurs

The browser doesn't show details about Script Errors because the JavaScript file causing the error is being served from a different domain or subdomain than the one where the HTML file is hosted.

This is known as a "cross-origin" request, and it is subject to the same-origin policy that restricts the ability of scripts to interact with resources from a different domain.

To protect users from malicious code, browsers restrict access to resources served from other domains and hide details on the error message, filename, line, or column where it happened.

How to solve the Script Error

The solution involves two steps: adding the "crossorigin" attribute to HTML script tags and setting the appropriate Cross-Origin Resource Sharing (CORS) HTTP header.

Both steps are necessary. They ensure that the browser allows the cross-origin request, and also that the server allows access to the resource from the requesting domain.

Step 1. Add "crossorigin" attribute to HTML script tags

First, add the "crossorigin" attribute to HTML script tags that reference external JavaScript files. This attribute tells the browser to allow cross-origin requests for that resource.

Here's an example of what it looks like (notice the crossorigin="anonymous" attribute):

<script crossorigin="anonymous" src="https://cdn.example.com/script.js"></script>

Step 2. Set the CORS HTTP header

Then, you should set the Cross-Origin Resource Sharing (CORS) HTTP header on the server that hosts the external JavaScript file.

This CORS header allows the server to specify which domains are allowed to access the resource. To set the CORS header, you can add the following code to your server's configuration file:

Access-Control-Allow-Origin: *

This code allows any domain to access the resource, but you can also specify specific domains by replacing the "*" with the domain name.


In conclusion, Script Errors can be frustrating and difficult to solve, but adding the "crossorigin" attribute to HTML script tags and setting the CORS HTTP header is a potential solution to help you fix this issue.

With this solution, you can ensure that your JavaScript files and resources can interact with each other seamlessly, regardless of their origin.

What are Core Web Vitals