How to stop caching with jQuery and javascript

本文介绍如何使用jQuery和JavaScript阻止浏览器缓存动态内容,确保显示最新版本。通过设置jQuery的ajax请求属性及在URL后附加随机数实现。

There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.

jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,

1$.ajax({
2url: 'test.html',
3cache: false,
4success: function(html){
5$('#results').append(html);
6}
7});

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,

1$.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.

Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,

1var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
2return $(img).load(function()
3{
4alert('completed!');
5});

This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.

HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,

1<meta http-equiv='cache-control' content='no-cache'>
2<meta http-equiv='expires' content='0'>
3<meta http-equiv='pragma' content='no-cache'>

This will prevent the whole page from being cached by the browser as well.

- See more at: http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/#sthash.H1QTE3vm.dpuf

There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.

jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,

1$.ajax({
2url: 'test.html',
3cache: false,
4success: function(html){
5$('#results').append(html);
6}
7});

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,

1$.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.

Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,

1var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
2return $(img).load(function()
3{
4alert('completed!');
5});

This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.

HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,

1<meta http-equiv='cache-control' content='no-cache'>
2<meta http-equiv='expires' content='0'>
3<meta http-equiv='pragma' content='no-cache'>

This will prevent the whole page from being cached by the browser as well.

- See more at: http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/#sthash.H1QTE3vm.dpuf

There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.

jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,

1$.ajax({
2url: 'test.html',
3cache: false,
4success: function(html){
5$('#results').append(html);
6}
7});

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,

1$.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.

Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,

1var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
2return $(img).load(function()
3{
4alert('completed!');
5});

This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.

HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,

1<meta http-equiv='cache-control' content='no-cache'>
2<meta http-equiv='expires' content='0'>
3<meta http-equiv='pragma' content='no-cache'>

This will prevent the whole page from being cached by the browser as well.

- See more at: http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/#sthash.H1QTE3vm.dpuf

There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.

jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,

1$.ajax({
2url: 'test.html',
3cache: false,
4success: function(html){
5$('#results').append(html);
6}
7});

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,

1$.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.

Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,

1var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
2return $(img).load(function()
3{
4alert('completed!');
5});

This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.

HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,

1<meta http-equiv='cache-control' content='no-cache'>
2<meta http-equiv='expires' content='0'>
3<meta http-equiv='pragma' content='no-cache'>

This will prevent the whole page from being cached by the browser as well.

- See more at: http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/#sthash.H1QTE3vm.dpuf
There are many reason people want to disable or stop caching by broswer. Especially when we are dealing with dynamic content that required the latest version to be displayed on the browser. However, due to security reason there are no perfect methods in javascript that can disabled caching for all browsers. In this tutorial, i will demonstrate a few way both javascript and jQuery used to stop or disable caching by browsers.
jQuery

On the later version of jQuery v1.2 above, jQuery has provided a method to stop caching by browser with its ajax class. You can visit jQuery website to see the list of update on v1.2 and you will notice that they have now included a function to disable caching! You can either choose to control the way each individual dynamic content cached by setting the properties to true/false or you can just set a default to disabled all browser caching.

In order to determine each ajax call caching properties,
1    $.ajax({
2    url: 'test.html',
3    cache: false,
4    success: function(html){
5    $('#results').append(html);
6    }
7    });

which is being reflected on the jQuery example. And in order to disable all caching by the browser we can do the following,
1    $.ajaxSetup({cache: false}});

This will have to be placed on top of the script in order for it to work.
Javascript

The reason why browsers are able to cache a particular document is due to the url being passed to the browser are identical. In order to make it unique for each passes, we can place in a random number behind the url as shown below,
1    var img.src = 'www.hungred.com'+'?'+Math.random()*Math.random();
2    return $(img).load(function()
3    {
4    alert('completed!');
5    });
This method will ensure all document to be unique for every dynamic passes you throw to the browser which i find it more reliable and useful as this method has been there for quite sometimes and almost all of the browser will support such way of retrieving dynamic content.
HTML (16/04/2009)

You can also disable or stop caching using  the following meta tag,
1    <meta http-equiv='cache-control' content='no-cache'>
2    <meta http-equiv='expires' content='0'>
3    <meta http-equiv='pragma' content='no-cache'>
This will prevent the whole page from being cached by the browser as well.


【电动车】基于多目标优化遗传算法NSGAII的峰谷分时电价引导下的电动汽车充电负荷优化研究(Matlab代码实现)内容概要:本文围绕“基于多目标优化遗传算法NSGA-II的峰谷分时电价引导下的电动汽车充电负荷优化研究”展开,利用Matlab代码实现优化模型,旨在通过峰谷分时电价机制引导电动汽车有序充电,降低电网负荷波动,提升能源利用效率。研究融合了多目标优化思想与遗传算法NSGA-II,兼顾电网负荷均衡性、用户充电成本和充电满意度等多个目标,构建了科学合理的数学模型,并通过仿真验证了方法的有效性与实用性。文中还提供了完整的Matlab代码实现路径,便于复现与进一步研究。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事智能电网、电动汽车调度相关工作的工程技术人员。; 使用场景及目标:①应用于智能电网中电动汽车充电负荷的优化调度;②服务于峰谷电价政策下的需求侧管理研究;③为多目标优化算法在能源系统中的实际应用提供案例参考; 阅读建议:建议读者结合Matlab代码逐步理解模型构建与算法实现过程,重点关注NSGA-II算法在多目标优化中的适应度函数设计、约束处理及Pareto前沿生成机制,同时可尝试调整参数或引入其他智能算法进行对比分析,以深化对优化策略的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值