使用javascript重置meta标签以防止其刷新页面
我们有一个遗留应用程序(经典 ASP 和 ASP.net 的混合),其中有一些 Ajax 内容丰富的页面。期望站点的用户在页面上执行一组可以跨越相当长的时间的任务,比如,15-30分钟。
其中一项要求是登录站点的用户在 15 分钟不活动后自动注销。目前,这是通过使用元标记在页面不活动 15 分钟后将用户重定向到注销页面来实现的。
我们遇到的问题是浏览器不认为尽管与服务器进行了多次 AJAX 交互,但页面上仍存在任何活动。因此,在浏览器认为不活动的 15 分钟后,即使用户正在执行某些操作,也会自动注销。
受到此留言板帖子的启发,我们尝试修复像这样使用 javascript(JQuery) 的烦恼
下面是一个事件处理程序,例如单击页面上的保存等。为简单起见,这里是页面加载以将刷新时间修改为 5 秒
$(document).ready(function() {
var selector = 'meta[name=Refresh]';
var content = $(selector).attr("content"); //"900;URL=/someurl/logout.asp"
$(selector).attr("content", "5;URL=/someurl/logout.asp");
});
意图是(重新)设置元标记内容页面刷新计时器将被重置。不幸的是这似乎不起作用(在 IE 中)。
由于这是一个遗留应用程序,因此一些决定(即使用元标记等)已被纳入其中。问题是,是否有一种方法可以使元标记刷新与 Ajax 应用程序和平共存?我做错了什么吗?有办法解决这个问题吗?
We have an legacy application (mixture of classic ASP and ASP.net) that has a few Ajax content rich pages.The expectation is that the users of the site performs a set of tasks on the page that can span a fair amount of time say, 15-30 minutes.
One of the requirements is that users logging in to the site be automatically logged out after 15 mins of inactivity. This is currently being achieved by using the meta tags for redirecting the user to the logout page after 15 minutes of inactivity in the page.
<meta name='Refresh' http-equiv="Refresh" content="900;URL=/someurl/logout.asp">
The problem we're having is that the browser doesnt think there has been any activity on the page despite having many AJAX interactions with the server. So after the 15 minutes of what the browser thinks as inactivity, the user is logged out automatically even if they are in the middle of doing something.
Inspired by this message board post we tried to fix the annoyance by using javascript(JQuery) like so
The below would be an event handler such as clicking of the save on the page etc. for simplicity here is the page load to modify the refresh time to 5 seconds
$(document).ready(function() {
var selector = 'meta[name=Refresh]';
var content = $(selector).attr("content"); //"900;URL=/someurl/logout.asp"
$(selector).attr("content", "5;URL=/someurl/logout.asp");
});
The intent being (re)setting the meta tag content the page refresh timer would be reset. Unfortunately this doesnt seem to work (in IE).
Since this is a legacy application, some of the decisions i.e. to use meta tags etc. are baked in. The question is, is there a way to get meta tag refresh to peacefully co-exist with an Ajax application? Am I doing something wrong and is there a way to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设你正在使用 jQuery,这样的东西可能会起作用。我还没有测试过它,也不知道浏览器是否会读取文档头中 noscript 元素内的元标记。
用这个替换你的元标记:
肯定有更好的方法来做到这一点。就像提供 2 个页面一样……一个为使用 JavaScript 的用户提供,另一个为不使用 JavaScript 的用户提供。
或者只是为没有 JavaScript 的人禁用该应用程序。 :o)
编辑
根据此页面:http://www .google.com/support/forum/p/Webmasters/thread?tid=25c023fea4ea60a4&hl=en 元刷新应该在 noscript 元素内工作。
assuming you are using jQuery something like this might work. I haven't tested it and don't know if browsers will read the a meta tag within a noscript element in the document head.
Replace your meta tag with this:
There are definitely better ways of doing this. Like serving 2 pages...one for users with javascript and one without.
Or just disable the app for people without javascript. :o)
Edit
according to this page: http://www.google.com/support/forum/p/Webmasters/thread?tid=25c023fea4ea60a4&hl=en the meta refresh should work inside the noscript element.
我认为没有办法阻止元刷新。您可以更改元标记,但浏览器已经决定将在 15 分钟后刷新;没有 API 可以阻止这种情况。
如果您无法停止刷新或使用 JavaScript 进行重定向,也许您可以控制损坏:
I don't think there's a way to stop the meta refresh. You can change the meta tag, but the browser has already decided it's going to refresh in 15 minutes; there's no API to stop that.
If you can't stop the refresh or use JavaScript to do the redirect, maybe you can contain the damage: