Home > Web Front-end > JS Tutorial > How Can I Execute External JavaScript from GitHub Despite MIME Errors?

How Can I Execute External JavaScript from GitHub Despite MIME Errors?

Mary-Kate Olsen
Release: 2024-11-25 20:04:12
Original
870 people have browsed it

How Can I Execute External JavaScript from GitHub Despite MIME Errors?

Execute External JavaScript from GitHub Despite MIME Error

When attempting to reference a local JavaScript file from GitHub's raw version, users may encounter an error. This arises from GitHub's use of X-Content-Type-Options: nosniff, which enforces strict MIME type checking. As a result, the linked script becomes unusable.

Fortunately, jsdelivr.net offers a simple workaround. By employing a specific set of modifications to the GitHub URL, users can seamlessly load external JavaScript.

Procedure:

  1. Obtain the "Raw" URL of the file from GitHub.
  2. Replace raw.githubusercontent.com with cdn.jsdelivr.net.
  3. Insert /gh/ before your GitHub username.
  4. Omit the branch name.
  5. Optionally, include the desired version using @version. Default behavior is to retrieve the latest version.

Examples:

  • Latest version:

    http://cdn.jsdelivr.net/gh/<username>/<repo>/path/to/file.js
    Copy after login
  • Specific version or commit hash:

    http://cdn.jsdelivr.net/gh/<username>/<repo>@<version or hash>/path/to/file.js
    Copy after login

Considerations:

  • For production environments, linking to a specific tag or commit hash is recommended over the branch. This prevents caching issues with subsequent version updates.
  • This workaround is a consequence of GitHub's strict MIME type enforcement policy, which prevents modern browsers from executing scripts from source files.

The above is the detailed content of How Can I Execute External JavaScript from GitHub Despite MIME Errors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template