简体   繁体   中英

Load script from different server/host , rather than my

I'm looking for the place where i can put my js script file. I'm creating javascript code like an Google Analitics code, so it will load an .js file, but my server cannot handle so many requests. I'm thinking to upload my script to another server. Maybe it is possible and legal? For example github? goolgle disk? Thanks.

Some tests loads .js from raw.github, you would call to it in the same way than local scripts:

<!-- local -->
<script src="animation.js"></script>

<!-- remote -->
<script src="https://raw.githubusercontent.com/PRDeving/HTMLAnimation/master/animation.js"></script>

Unless you have acces to the host server and allows cross origin, you gonna have to ask for the remotes from a secure server (https) or it wont work.

It's legal and easy but, if it's a final app you should go for a statics server like amazon WS.

I don't think that Google accepts scripts without STRONG backup info about and testing. jQuery is hosted on MAX CDN, they offer a free trial, and in the future for more requests or number of files you have to pay. Here is the link

MAXCDN

You can chose to pay per bandwidth or a subscription by month, the lowest being:

100GB bandwidth/month

$9 per month

8¢ per GB overage*

2 websites (zones)

Hope it helps.

(I know this is an old thread - but Google popped this up while I was searching for something tangentially related, so thought it could use some updates on more recent options)

I'm thinking to upload my script to another server. Maybe it is possible and legal? For example github? goolgle disk? Thanks.

  • Possible: yes - if permitted
  • Legal: yes - if permitted ☺

Web servers set couple of header fields called Cross-Origin-Resource-Sharing (Cors) (read more here: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin ) If the server has been configured to allow it's files to be pulled into other sites (ie, sets the Cors header to a permissive value), then it will work.

Web sites also set a header field to prevent user injection from untrusted locations. This is called Content-Security-Policy (CSP) - Facebook for example sets one that all images and scripts must come from Facebooks servers. (Read more on setting this header in a JavaScript app here: https://www.digitalocean.com/community/tutorials/how-to-secure-node-js-applications-with-a-content-security-policy )

Some web servers that I'm aware of that are permissive in this manner:

  • raw.githubusercontent.com (the raw pages on GitHub)
  • GitHub Pages (if you put your source in GitHub and turn on GitHub Pages, by default Cors is allowed - at least for now)
  • Any CDN (Content-Delivery-Network) server: There are a bunch of these and they range in price vs guaranteed up time (google it if the following don't meet your needs)
    • jsdelivr.com - a free CDN for anything in the npm repo, on GitHub, or WordPress plugins
    • Cloudflare (free plan or paid)
    • Stackpath.com (what JQuery uses at present - paid plans only it looks like)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM