Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript to reset the meta tag to prevent it from refreshing the page

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?

like image 701
Dilip Krishnan Avatar asked Feb 05 '10 20:02

Dilip Krishnan


People also ask

How do I fix meta refresh?

How to fix this issue. Delete the tag <meta http-equiv=”refresh” content=”10″> and apply redirect 301 for forwarding to the right page. Check inner links, they must lead to the page a user is forwarded to from the current one. Page A link to -> bad Page -> 301 redirect to ->PageB.

How do you refresh HTML automatically with the help of meta tags?

Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag.

What is the refresh meta tag used for?

HTML allows for the specification of meta information within META tags. A popular use of this technique involves specifying redirections or page reloads within HTML code, rather than relying on HTTP headers to do so (for example, HTTP status code 302 Document moved for redirections).

What is meta http-equiv Refresh?

Meta refresh is a method of instructing a web browser to automatically refresh the current web page or frame after a given time interval, using an HTML meta element with the http-equiv parameter set to " refresh " and a content parameter giving the time interval in seconds.


1 Answers

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.

like image 63
David Murdoch Avatar answered Sep 19 '22 15:09

David Murdoch