TechBuzzPro

Script Error Details: Why Your Error Tracker Shows Them

· 7 min read · Apps & Browsers

In this article
  1. What a Script Error Actually Is
  2. Why Browsers Hide the Details
  3. How to Reveal the Error Details
  4. Common Problems After Adding crossorigin
  5. Alternative Approaches to Get Error Details
  6. How to Test If Your Fix Works
  7. Frequently Asked Questions

Your error tracker shows "Script error" because browsers deliberately hide the real error details for cross-origin scripts to protect user privacy, and you can reveal them by adding the `crossorigin="anonymous"` attribute to your script tags and setting the `Access-Control-Allow-Origin` header on the server that serves those scripts.

What a Script Error Actually Is

When you see "Script error" in your error tracking tool (like Sentry, LogRocket, or your own custom logger), it means the browser threw an error from a JavaScript file that lives on a different origin (domain, protocol, or port) than your page. The browser masks the error message, the file name, and the line number, leaving only the generic text "Script error." This behavior is specified in the HTML standard and is enforced by all major browsers. It exists to prevent a malicious page from extracting sensitive information from a third-party script, such as a user's session token or personal data that might be embedded in an error message.

For example, if your site at example.com loads a script from cdn.example.net and that script throws a TypeError, your window.onerror handler will receive a message of "Script error" instead of "Cannot read property 'foo' of undefined". The same happens for unhandled promise rejections and for errors caught by window.addEventListener('error', ...).

Why Browsers Hide the Details

The masking is part of the Same-Origin Policy. If a script from another origin could report its internal errors to your page, it could leak information about the user's interaction with that other origin. For instance, if a user is logged into a banking site and a script from that site is loaded on your page, an error message might reveal the user's account number or balance. To prevent this, the browser strips all identifying details from any error that originates from a cross-origin script, unless the script explicitly opts in to sharing that information.

There are two conditions for the browser to share the full error details:

  • The script tag must have a crossorigin attribute (usually crossorigin="anonymous").
  • The server that serves the script must respond with an HTTP header Access-Control-Allow-Origin that includes your site's origin (or *).

If either condition is missing, the error remains masked. This is not a bug; it's a security feature that you must work with, not against.

How to Reveal the Error Details

To get the real error message, file, and line number, you need to make the cross-origin script a CORS-enabled resource. Here are the exact steps:

  1. Add crossorigin="anonymous" to every <script> tag that loads a script from another origin. For example: <script src="https://cdn.example.com/lib.js" crossorigin="anonymous"></script>.
  2. On the server that hosts the script, add the HTTP response header Access-Control-Allow-Origin: https://yourdomain.com (or * if you want any site to read the error details, which is usually fine for public libraries).
  3. If you use a module script (type="module"), the crossorigin attribute is implied, but you still need the server to send the CORS header.
  4. If you load scripts dynamically via JavaScript (e.g., document.createElement('script')), set the crossOrigin property to anonymous before appending to the DOM.

After these changes, your error tracker will receive the full error object with the message, stack trace, and source file. Note that if the script is served from a CDN that you don't control, you cannot change its headers; in that case, you can either proxy the script through your own server or accept that you won't get details for that particular script.

Common Problems After Adding crossorigin

Adding crossorigin="anonymous" can sometimes break the script if the server doesn't send the CORS header. The browser will block the script entirely, and you'll see a network error in the console. This is worse than a masked error. To avoid this, test the header first using the browser's developer tools or a command-line tool like curl -I https://cdn.example.com/lib.js to see if Access-Control-Allow-Origin is present.

Another issue: if the script is served from a different origin but uses cookies for authentication (e.g., a personalized analytics script), adding crossorigin="anonymous" will prevent cookies from being sent, because the anonymous credential mode disables credentials. In that case, you can use crossorigin="use-credentials", but then the server must respond with Access-Control-Allow-Credentials: true and a specific origin (not *). This is more complex and only necessary for scripts that need cookies.

Also, note that some older browsers (like IE10 and below) do not support the crossorigin attribute, so they will continue to show "Script error" regardless. For those browsers, you can use a polyfill or simply accept the limitation.

Alternative Approaches to Get Error Details

If you cannot modify the script's server or the script tag, there are a few workarounds, though they have trade-offs:

  • Wrap the script in a try-catch inside the external file: If you control the source of the external script, you can wrap the entire file in a try-catch and call a global error handler with the details. This bypasses the browser's masking because the error is caught and re-thrown in the same origin context.
  • Use a proxy: Serve the external script from your own domain by proxying it. This makes it same-origin, so errors are not masked. However, you lose the benefits of a CDN and must handle caching.
  • Use a global error handler that captures the event object: In some browsers, the ErrorEvent object passed to window.onerror may contain a message property that is still masked, but the error property (the actual Error object) might be available if the browser provides it. In Chrome, for cross-origin scripts without CORS, the error property is null, so this doesn't help.

None of these are as clean as the CORS approach, but they can be useful when you have no control over the third-party script.

How to Test If Your Fix Works

After making the changes, you should verify that your error tracker now receives full details. Here's a simple test:

  1. Create a test page that includes a script from a different origin (e.g., a simple script that throws an error).
  2. Before the fix, open the browser console and observe that the error message is "Script error".
  3. Add the crossorigin attribute and the CORS header, then reload and check the console. You should see the actual error message and stack trace.
  4. If you use an error tracking service, send a test error and check the dashboard to confirm the details appear.

Remember that the CORS header must be present on the response that serves the script file, not on your page. If you are using a CDN like jsDelivr or unpkg, they already send Access-Control-Allow-Origin: *, so adding crossorigin="anonymous" is sufficient. For other CDNs, check their documentation.

Frequently Asked Questions

Why do I see "Script error" even for same-origin scripts?

If the script is actually same-origin, you should not see "Script error" unless the script is served from a different origin than the page. Double-check the URL: if the page is on www.example.com and the script is on example.com (without www), that's a cross-origin request. Also, if you are using a service worker that redirects to a different origin, the browser may treat it as cross-origin.

Can I use crossorigin="anonymous" on inline scripts?

No, the crossorigin attribute only applies to external script files. Inline scripts are always same-origin, so they never produce "Script error" due to cross-origin issues. If you see "Script error" from an inline script, it's likely because the script is actually loaded from an external file.

Does this affect performance or security?

Adding crossorigin does not affect performance significantly. It does make the script a CORS request, which may add an extra preflight request in some cases, but for simple GET requests with no custom headers, there is no preflight. Security-wise, you are explicitly opting in to share error details, which is safe as long as you trust the script and the server. It doesn't expose any user data beyond what the script itself could already access.

What if the third-party script doesn't send CORS headers?

Then you cannot get the error details directly. Your options are to proxy the script, wrap it in a try-catch if you control the source, or contact the script provider and ask them to add the header. Many popular CDNs already send Access-Control-Allow-Origin: *, so it's often a non-issue for well-known libraries.

Is there a way to get error details without changing the script tag?

You could use a service worker to intercept the script request and add the CORS header yourself, but this is complex and may not work in all browsers. The simplest and most reliable method is to add the crossorigin attribute and ensure the server sends the header.