使用Html5boilerplate的代码在本地运行时加载JQuery的速度非常慢 [英] Using Html5boilerplate's code to load JQuery very slow when run locally

查看:101
本文介绍了使用Html5boilerplate的代码在本地运行时加载JQuery的速度非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用以下代码在我的所有项目中加载JQuery.我从 http://html5boilerplate.com/抓取了它.

I have been using the following code for loading JQuery in all of my projects. I grabbed it from http://html5boilerplate.com/. There is extensive disussion of this technique here.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="jquery-1.7.1.min.js"><\/script>');</script>

此代码很好用,并且一旦将其放在互连网上就显得相当快,但是当我在本地打开我的.html文件时,每次刷新大约需要10秒钟.通常,我受够了,并按如下所示更改代码:

This code works great and seems pretty darn quick once I've put it up on the interwebs, but when I open my .html file locally it takes ~10 seconds per refresh. Generally I get fed up and alter the code as follows:

<!-- uncomment when going live 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="jquery-1.7.1.min.js"><\/script>');</script>-->

<!-- remove following line when going live -->
<script src="jquery-1.7.1.min.js"></script>

我在这里缺少明显的东西吗?我觉得我不应该获得超慢的加载时间,但是当注释掉这些行时,它确实可以解决问题.

Am I missing something obvious here? I feel like I should not be getting the super-slow loading times, but it does resolve itself when comment out those lines.

推荐答案

我猜测您不是通过Web服务器提供HTML.

I'm guessing that you're not serving the HTML through a web server.

URL上的//前缀表示它应该使用与当前资源相同的协议(通常是http或https)

The // prefix on the url indicates that it should use the same protocol as the current resource (usually either http or https)

由于您不是通过http服务,而是通过文件服务,因此它会尝试在本地文件系统上寻找它,最终导致超时.

Since you're not serving through http and instead through a file, it's trying to look for it on your local file system, eventually timing out.

Chrome检查器上的网络"标签显示了它试图为我加载以下内容:

The network tab on Chrome inspector shows it trying to load the following for me:

file://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
文件:///C:/Users/[我的用户名]/Documents/jquery-1.7.1.min.js

file://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
file:///C:/Users/[My Username]/Documents/jquery-1.7.1.min.js

它将尝试加载这些时间,文件系统(或可能是浏览器)最终将超时.

It'll try to load those times and the file system (or maybe the browser) will eventually timeout.

正确的方法是通过Web服务器(如果使用Windows,则为IIS;如果使用Linux/Mac,则为Apache)(Apache也可在Windows中工作,但IIS具有更好的UI工具)来为它提供服务

The proper way is to serve it through a web server, either IIS if you're on Windows or Apache if you're on Linux/Mac (Apache also works in Windows, but IIS has better UI tools)

这篇关于使用Html5boilerplate的代码在本地运行时加载JQuery的速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆