使用javascript重置元标记以防止它刷新页面

时间:2022-02-15 06:28:07

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.

我们有一个遗留应用程序(经典ASP和ASP.net的混合),它有一些Ajax内容丰富的页面。期望网站的用户在页面上执行一系列任务,可以跨越相当长的时间说,15-30分钟。

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.

其中一个要求是登录到站点的用户在15分钟不活动后自动注销。目前,这是通过使用元标记在页面中不活动15分钟后将用户重定向到注销页面来实现的。

<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.

我们遇到的问题是,尽管与服务器进行了许多AJAX交互,浏览器仍然认为页面上没有任何活动。因此,在浏览器认为不活动的15分钟之后,即使用户正在做某事,用户也会自动注销。

Inspired by this message board post we tried to fix the annoyance by using javascript(JQuery) like so

受此留言板帖子的启发,我们试图通过使用javascript(JQuery)来解决烦恼

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

下面将是一个事件处理程序,例如单击页面上的保存等。为简单起见,此处是页面加载,将刷新时间修改为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"); 
});

The intent being (re)setting the meta tag content the page refresh timer would be reset. Unfortunately this doesnt seem to work (in IE).

将重置(重新)设置页面刷新计时器的元标记内容的意图。不幸的是,这似乎不起作用(在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?

由于这是一个遗留应用程序,因此一些决策,即使用元标记等等都被烘焙。问题是,有没有办法让元标记刷新与Ajax应用程序和平共存?我做错了什么,有没有办法解决这个问题?

2 个解决方案

#1


7  

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.

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

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.

肯定有更好的方法可以做到这一点。就像服务2页一样...一个用户使用javascript,一个用户没有。

Or just disable the app for people without javascript. :o)

或者只是禁用没有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.

根据此页面进行编辑:http://www.google.com/support/forum/p/Webmasters/thread?tid = 25c023fea4ea60a4&hl = zh_cn元刷新应该在noscript元素内部工作。

#2


0  

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.

我认为没有办法阻止元刷新。您可以更改元标记,但浏览器已经确定它将在15分钟内刷新;没有API可以阻止它。

If you can't stop the refresh or use JavaScript to do the redirect, maybe you can contain the damage:

如果你无法停止刷新或使用JavaScript进行重定向,也许你可以包含损坏:

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

#1


7  

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.

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

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.

肯定有更好的方法可以做到这一点。就像服务2页一样...一个用户使用javascript,一个用户没有。

Or just disable the app for people without javascript. :o)

或者只是禁用没有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.

根据此页面进行编辑:http://www.google.com/support/forum/p/Webmasters/thread?tid = 25c023fea4ea60a4&hl = zh_cn元刷新应该在noscript元素内部工作。

#2


0  

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.

我认为没有办法阻止元刷新。您可以更改元标记,但浏览器已经确定它将在15分钟内刷新;没有API可以阻止它。

If you can't stop the refresh or use JavaScript to do the redirect, maybe you can contain the damage:

如果你无法停止刷新或使用JavaScript进行重定向,也许你可以包含损坏:

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