(Unknown): Script Error in JavaScript
Published: 2022-04-07 13:33:58



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://cdn-images-1.medium.com/max/2000/0*VWtBvU3gxypT4X-H.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://cdn-images-1.medium.com/max/2000/1*VfjlPDJPnenFS62w5Duhiw.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?

## I am facing an Script Error In JavaScript, What Should I Do?

If you also face the same issue, then go ahead with these possible solutions.

## 1. Set up Cross-Origin HTTP header

    Access-Control-Allow-Origin: *

 By setting Access-Control-Allow-Origin: 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.



Note: [MultipleFailureException](https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.junit.runners.model.MultipleFailureException?utm_source=devto&utm_medium=organic&utm_campaign=apr07_sd&utm_term=sd&utm_content=webpage) happens when Junit runner recognize many failures and collect them into one.

## 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:

    Header add Access-Control-Allow-Origin "*"

* Ngnix: Add add_header directive as:

    location ~ ^/assets/ {
        add_header Access-Control-Allow-Origin *;
    }

* HA Proxy: Add this asset:

    rspadd Access-Control-Allow-Origin:\ *

Note: [InvalidOrderingException](https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.junit.runner.manipulation.InvalidOrderingException?utm_source=devto&utm_medium=organic&utm_campaign=apr07_sd&utm_term=sd&utm_content=)  happens when test execution ordering is wrong and junit runner is not able to invoke them.


## 2. Set crossorigin=”anonymous”

For every script in your HTML script, that you’ve set Access-Control-Allow-Origin, set crossorigin="anonymous"


    <script src="[http://another-domain.com/app.js](http://another-domain.com/app.js)" crossorigin="anonymous"></script>

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.