(Unknown): Script Error in JavaScript
Tags: javascript, coding, beginners
Published: 2019-01-30 15:15:06

---
title: (Unknown): Script Error in JavaScript
published: true
description: When an exception violates the same origin policy of a browser in response to onerror callback, the browser responds with a ‘script error’.
tags: #javascript #coding #beginners
cover_image: https://www.lambdatest.com/blog/wp-content/uploads/2018/03/SE.jpg
canonical_url: https://www.lambdatest.com/blog/script-error-in-javascript/
---

If someone tries to violate the rule that you have set, what you’ll do in return? Send him Script error.

Not in real life, though. 😉

But in JavaScript, if you try to violate the same origin policy, the browser will send you ‘Script error’ in return.

![](https://www.lambdatest.com/blog/wp-content/uploads/2018/03/Unknown-Script-Error-in-JavaScript.png)

###When Script Error Occurs In A JavaScript Code?###
When an exception violates the same origin policy of a browser in response to onerror callback, the browser responds with a ‘script error’.

Same Origin policy: According to same origin policy, the browser accepts only the scripts hosted on the same server on two different web pages.

Every browser has a set of acceptable or say, required ‘Request Headers’. When you hit any request on the server, it should contain those essentials for request header. Else you are going to face an script error.

![](https://www.lambdatest.com/blog/wp-content/uploads/2018/03/Script-error.png)

Well, this is an intentional behaviour by the browsers in order to prevent scripts from leaking to external domains. As no one wants to entertain the unwanted requests 😉 Do you?

<strong>I am facing an Script Error In JavaScript, What Should I Do?</strong>
If you also face the same issue, then go ahead with these possible solutions.

<strong>1. Set up Cross-Origin HTTP header</strong>
<code>Access-Control-Allow-Origin: *</code>

By setting <code>Access-Control-Allow-Origin:</code> to ‘ * ’, you make sure that you can access the resource from any domain. If necessary, you can also replace the ‘ * ‘ by the specific domain name, which you want that should access your domain’s script.

There is different method to set his to * in different environments.

* Apache: Create an .htaccess file in the folder where your JS file is served with:
<code>Header add Access-Control-Allow-Origin "*"</code>
* Ngnix: Add add_header directive as:
<pre>location ~ ^/assets/ {
    add_header Access-Control-Allow-Origin *;
}
</pre>
* HA Proxy: Add this asset:
<code> rspadd Access-Control-Allow-Origin:\ * </code>

<strong>2. Set crossorigin=”anonymous”</strong>
For every script in your HTML script, that you’ve set Access-Control-Allow-Origin, set 
<code>crossorigin="anonymous"</code>
<pre> &lt;script src="http://another-domain.com/app.js" crossorigin="anonymous"&gt;&lt;/script&gt; 
</pre>

This code tells that your browser to fetch the target file anonymously avoiding transmission of any user identifying information like HTTP credentials or cookies while requesting by the browser.

If you face any script error in your JavaScript code, hope this article may prove to be of help.

[Originally Published Here](https://www.lambdatest.com/blog/script-error-in-javascript/).

Related Articles:
1. [Reference Error: JavaScript](https://www.lambdatest.com/blog/reference-error-javascript/)
2. [Common JavaScript Errors and How To Handle Them](https://www.lambdatest.com/blog/common-javascript-errors-and-how-to-handle-them/)
3. [Debugging JavaScript Using the Browser’s Developer Console](https://www.lambdatest.com/blog/debugging-javascript-using-the-browsers-developer-console/)