Home  >  Article  >  Web Front-end  >  How to Fix \"Refused to Load Script\" Error in Android 5.0.0 Apps?

How to Fix \"Refused to Load Script\" Error in Android 5.0.0 Apps?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-19 13:07:29834browse

How to Fix

Addressing the "Refused to Load Script" Error in Android 5.0.0 Devices

When deploying Cordova apps to Android devices running Lollipop or later, developers may encounter the "Refused to load the script" error. This error arises from the stricter Content Security Policy (CSP) implemented in these versions of Android.

The CSP directive ensures that scripts are loaded only from trusted sources. By default, it allows scripts from the origin of the web page ('self') and enables 'unsafe-eval' and 'unsafe-inline' for testing purposes. However, this policy can be restrictive when incorporating scripts from third-party sources.

To resolve this issue, developers can modify the CSP directive in the index.html file of their project. By adding the following line to the directive, they can specify additional trusted sources:

<code class="html"><meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; script-src 'self' http://[TrustedDomain] 'unsafe-inline' 'unsafe-eval'; "></code>

For instance, if the remote JavaScript file is located at http://Guess.What.com/MyScript.js, the corrected meta tag would be:

<code class="html"><meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; script-src 'self' http://Guess.What.com 'unsafe-inline' 'unsafe-eval'; "></code>

By incorporating this modification, the CSP policy explicitly trusts the remote source and allows the script to be loaded successfully.

The above is the detailed content of How to Fix \"Refused to Load Script\" Error in Android 5.0.0 Apps?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn