Javascript jQuery 是否只有 Ajax 部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there only Ajax part of jQuery?
提问by fgfgfgghjh
Is there only Ajax part of jQuery? The matter is I do not need the whole library but only its Ajax part (I need to include this part on several iframes).
jQuery 是否只有 Ajax 部分?问题是我不需要整个库,而只需要它的 Ajax 部分(我需要在几个 iframe 中包含这部分)。
回答by Neoaptt
Update 2016
2016 年更新
You can use this tool to build your own custom jQuery version.
您可以使用此工具构建您自己的自定义 jQuery 版本。
As of jQuery 2.1.1
从 jQuery 2.1.1 开始
Full file sized unminified is: 241.55 Kb
未缩小的完整文件大小为:241.55 Kb
Ajax Only minified is: 49.60 Kb
Ajax 仅缩小为:49.60 Kb
That is a 5xreduction in size.
那是尺寸减少了5 倍。
回答by Pekka
As Darin already says, it's all or nothing. JQuery's Ajax functions are closely intertwined with the rest of the functionality.
正如达林已经说过的,要么全有要么全无。JQuery 的 Ajax 函数与其他功能紧密地交织在一起。
There are a few other, stand-alone Ajax libraries around like Matt Kruse's Ajax toolbox- maybe that helps.
还有其他一些独立的 Ajax 库,比如Matt Kruse 的 Ajax 工具箱——也许这会有所帮助。
I would consider loading the full jQuery library. If you link to jQuery on a CDN,loading times will be minuscule.
回答by Daniel Waltrip
Another option would be to use the built-in fetch
API provided by the browser.
另一种选择是使用fetch
浏览器提供的内置API。
Here is an example snippet:
这是一个示例片段:
fetch('http://localhost:3000/users.json', {
method: 'POST',
mode: 'cors',
redirect: 'follow',
body: JSON.stringify({
user: {
firstName: 'john',
lastName: 'doe'
}
}),
headers: new Headers({ 'Content-Type': 'application/json' })
}).then(function() {
/* handle response */
});
This blog postis a great introduction to the API and shows more use cases.
这篇博文很好地介绍了 API,并展示了更多用例。
fetch
doesn't have full cross-browser support yet (I think mainly IE and Safari are lacking), but there is polyfill that you can use until that day comes.
fetch
还没有完全的跨浏览器支持(我认为主要是 IE 和 Safari 缺乏),但是在那天到来之前你可以使用 polyfill。
fetch
polyfill: https://github.com/github/fetch
fetch
polyfill:https: //github.com/github/fetch
Older browsers will also need a Promise
polyfill (one option, another option).
回答by William Isted
You can view standard javascript alternatives to jQuery at youmightnotneedjquery.com
您可以在youmightnotneedjquery.com查看 jQuery 的标准 javascript 替代品
For example the alternative to $.ajax
post
is:
例如,替代方案$.ajax
post
是:
var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
And the alternative to $.ajax
get
is:
替代方案$.ajax
get
是:
var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var resp = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
回答by Sista Fiolen
AMD user please read this, my answer is for building a single file.
Or just use this library: ded / reqwest(4 KB, min & gzip)
AMD 用户请阅读本文,我的答案是构建单个文件。
或者直接使用这个库:ded / reqwest(4 KB, min & gzip)
Download source codeand run
npn i
Open
/src/jquery.js
and remove any moudle you don't want, but keep"./exports/amd"
,"./exports/global"
define([ "./ajax", "./ajax/xhr", "./ajax/script", "./ajax/jsonp", "./exports/amd", "./exports/global" ], function (jQuery) { "use strict"; return jQuery; });
Run
grunt custom:-sizzle
Goto
/dist
and take your build
下载源代码并运行
npn i
打开
/src/jquery.js
并移除您不想要的任何模块,但保留"./exports/amd"
,"./exports/global"
define([ "./ajax", "./ajax/xhr", "./ajax/script", "./ajax/jsonp", "./exports/amd", "./exports/global" ], function (jQuery) { "use strict"; return jQuery; });
跑
grunt custom:-sizzle
转到
/dist
并获取您的构建
Modules in your build now:
现在构建中的模块:
- core
- deferred
- ajax
- 核
- 延期
- 阿贾克斯
Size:
尺寸:
- just build: 85 KB
- build with min: 26 KB
- build with min & gzip: 10 KB
- 刚刚构建:85 KB
- 最小构建:26 KB
- 使用 min & gzip 构建:10 KB
回答by Sam
I've created a custom build of jQuery 1.7.1 here:
我在这里创建了 jQuery 1.7.1 的自定义版本:
回答by Noypi Gilas
YES, I just did mine, http://noypi-linux.blogspot.com/2013/05/build-jquery-with-ajax-only.html
是的,我刚刚做了我的, http://noypi-linux.blogspot.com/2013/05/build-jquery-with-ajax-only.html
you only need these files (resulting minified is about 30Kb):
您只需要这些文件(缩小后的大小约为 30Kb):
/d/dev/javascript/jquery/jquery/src/intro.js
/d/dev/javascript/jquery/jquery/src/core.js
/d/dev/javascript/jquery/jquery/src/callbacks.js
/d/dev/javascript/jquery/jquery/src/deferred.js
/d/dev/javascript/jquery/jquery/src/support.js
/d/dev/javascript/jquery/jquery/src/data.js
/d/dev/javascript/jquery/jquery/src/event.js
/d/dev/javascript/jquery/jquery/src/serialize.js
/d/dev/javascript/jquery/jquery/src/ajax.js
/d/dev/javascript/jquery/jquery/src/ajax/xhr.js
/d/dev/javascript/jquery/jquery/src/exports.js
/d/dev/javascript/jquery/jquery/src/outro.js
回答by Adam Heath
If you really really want just the Ajax parts of jQuery you can get the code from their repository (https://github.com/jquery/jquery), glancing at it you would want to look at "ajax.js" and "core.js" in the "src" directory. You would then want to compile them together with the closure compiler or something.
如果你真的只想要 jQuery 的 Ajax 部分,你可以从他们的存储库 ( https://github.com/jquery/jquery) 中获取代码,浏览它你会想要查看“ajax.js”和“core” .js”在“src”目录中。然后,您可能希望将它们与闭包编译器或其他东西一起编译。
But as others stated, it would be a lot easier to just load it from one of the CDNs (jQuery, Google, Microsoft) which most users will have cached anyway.
但正如其他人所说,从大多数用户无论如何都会缓存的 CDN(jQuery、Google、Microsoft)之一加载它会容易得多。
回答by Darin Dimitrov
It's all or nothing. Of course jquery is open source and you could extract the part you are interested in in your own library (good luck with this). You may consider using a CDN which will ensure that most users will already have it cached in their browsers so you shouldn't be concerned about size.
要么全有要么全无。当然 jquery 是开源的,你可以在你自己的库中提取你感兴趣的部分(祝你好运)。您可以考虑使用 CDN,这将确保大多数用户已经将其缓存在他们的浏览器中,因此您不必担心大小。