使用javascript重置meta标签以防止其刷新页面

发布于 2024-08-20 14:27:56 字数 999 浏览 6 评论 0原文

我们有一个遗留应用程序(经典 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2024-08-27 14:27:56

假设你正在使用 jQuery,这样的东西可能会起作用。我还没有测试过它,也不知道浏览器是否会读取文档头中 noscript 元素内的元标记。

用这个替换你的元标记:

<script type="text/javascript">
    window.onload = function(){
        var timer = null,
            time=1000*60*15, // 15 minutes
            checker = function(){
                if(timer){clearTimeout(timer);} // cancels the countdown.
                timer=setTimeout(function() { 
                    document.location="/someurl/logout.asp";
                },time); // reinitiates the countdown.
            };
        checker(); // initiates the countdown.
        // requires jQuery... (you could roll your own jQueryless version pretty easily though)
        if(window.jQuery){
            // bind the checker function to user events.
            jQuery(document).bind("mousemove keypress click", checker);
        }
    };
</script>
<noscript>
    <meta name="Refresh" http-equiv="Refresh" content="900;URL=/someurl/logout.asp">
</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:

<script type="text/javascript">
    window.onload = function(){
        var timer = null,
            time=1000*60*15, // 15 minutes
            checker = function(){
                if(timer){clearTimeout(timer);} // cancels the countdown.
                timer=setTimeout(function() { 
                    document.location="/someurl/logout.asp";
                },time); // reinitiates the countdown.
            };
        checker(); // initiates the countdown.
        // requires jQuery... (you could roll your own jQueryless version pretty easily though)
        if(window.jQuery){
            // bind the checker function to user events.
            jQuery(document).bind("mousemove keypress click", checker);
        }
    };
</script>
<noscript>
    <meta name="Refresh" http-equiv="Refresh" content="900;URL=/someurl/logout.asp">
</noscript>

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.

情场扛把子 2024-08-27 14:27:56

我认为没有办法阻止元刷新。您可以更改元标记,但浏览器已经决定将在 15 分钟后刷新;没有 API 可以阻止这种情况。

如果您无法停止刷新或使用 JavaScript 进行重定向,也许您可​​以控制损坏:

$(window).unload(function() { 
  // Insert code here 
  // to save the user's work.
  alert('Sorry, you have to log out now.')
}); 

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:

$(window).unload(function() { 
  // Insert code here 
  // to save the user's work.
  alert('Sorry, you have to log out now.')
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文