if-else in body tag...

Hi there!

To avoid reaching server´s "idle" time, every 4min (without the user´s action) a javascript routine "refreshes" the page this (logged in) user is, so that he/she will not need to re-login. This works as expected.

My problem: some specific pages (where there are videos to be seen) should NOT be refreshed (ever), and I thought I could create something like this:

<% If "there-is-video-in-this-page" Then %>
  <body>
 <% Else %>
  <body onLoad="timeout_routine();">
<% End If %>  

But it seems as the onLoad attribute is happening with or without the existence of videos. The conditional "there-is-video-in-this-page" works as expected in other parts of the code, by the way...

Could you please explain to me why it does not work as expected and, of course, how to fix that?

Thanks a lot,
fskilnik
fskilnikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
not sure why this does not work, but perhaps an alternative:
<body <% If (Not thereisvideoinpage) Then Response.Write "onLoad='timeout_routine();' %>>

Open in new window


HTH
Rainer
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

How do you determine If there-is-video-in-this-page?

Try putting-
<% If True Then %>
and let us know the result!?!
fskilnikAuthor Commented:
Hi Rainer,

Thank you for your help.

Just for the record, of course I´ve tried:
<body <% If (Not thereisvideoinpage) Then Response.Write "onLoad='timeout_routine();'" %>>  

but testing it I found the same behavior that in my idea... the page refreshes with and without having videos... therefore the problem persists.

@Rikin... I will try now, and I will be back soon... thanks for your participation here.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

fskilnikAuthor Commented:
Uau... that´s really strange!

I´ve tried this:

<body <% If 1 = 2 Then Response.Write "onLoad='timeout_routine();'" %>>

And the "routine" started...  how is that possible?
Big MontyWeb Ninja at largeCommented:
do you have a link to the page or can you paste the rendered html here? if you have

<body <% If 1 = 2 Then Response.Write "onLoad='timeout_routine();'" %>>

then look at the source html, you should not see a call to that function, so my guess is something else is causing the reload
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi again,

You can also try like this after your body tag-

<%
If message = "This is my message" Then
Response.Write("<script language='javascript'>functionCall();</script>") 
End If
%>

Open in new window

fskilnikAuthor Commented:
Hi, Big Monty.

Thanks for joining!   :)

I had the same impression, therefore I changed it to "<body>" and tested... it reloaded. That means you are absolutely right...

There are three places related to this routine, I guess:

1.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <!--jQuery related to that-->

2.
<script type="text/javascript" language="javascript">
      $(function () {
    var pageTimeout

        function resetTimer() {
            clearTimeout(pageTimeout);
            pageTimeout = setTimeout(timeoutExpired,  240000);
        }
    $(document.body).bind('mousemove,keydown,click', resetTimer);
    resetTimer();

    function timeoutExpired() {
        window.location.reload() // if not activity for 4 minutes reload page, 1min = 60 000
    }
});
</script>  

3.  The <body ... >  place...
fskilnikAuthor Commented:
Hi Rikin!

Good idea... but I guess I have to stop the routine at the first place...
fskilnikAuthor Commented:
I took out both 1. and 2. above, let us see if there is something else running the routine... just one moment, please.
fskilnikAuthor Commented:
No. Now it did not reload... let me ask you... item 2. could run without being called?
Big MontyWeb Ninja at largeCommented:
the line

 $(function () {

essentially says, fire off this javascript code when the page loads. besides declaring a variable, defining some functions, and binding a function to a mouse click, you're also calling resetTimer(), which fires off the page reload function!

I would remove your IF statements on the body tag, and move them to this code block:

<script type="text/javascript" language="javascript">
      $(function () {
    var pageTimeout

        function resetTimer() {
            clearTimeout(pageTimeout);
            pageTimeout = setTimeout(timeoutExpired,  240000);
        }
    $(document.body).bind('mousemove,keydown,click', resetTimer);
    
    <% If (Not thereisvideoinpage) Then %>
           resetTimer();
    <% end if %>

    function timeoutExpired() {
        window.location.reload() // if not activity for 4 minutes reload page, 1min = 60 000
    }
});
</script>   

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fskilnikAuthor Commented:
After many tests, I guess the answer to my last question is a strange "yes"... the script in 2. runs even without any other reference to "timeoutExpired in the whole page...
fskilnikAuthor Commented:
Big Monty... you are THE one!!!!    I will do that now...
fskilnikAuthor Commented:
PERFECT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks A LOT (as always) Big Monty. You should already be my partner here, financially speaking. The amount of things you have already fixed in my tasks is HUGE....

Well, I guess it will be hard to be fair in the division of points, because the solution was really far from the question... I hope everyone will be pleased.

Thanks a lot Rainer, Rikin and Big Monty!!

Regards,
fskilnik
Big MontyWeb Ninja at largeCommented:
hey I'm always up for donations :) j/k of course!

glad it got resolved either way
fskilnikAuthor Commented:
Please think about a "wish list" in amazon.com, for instance. Near Xmas I WILL go there for sure...   :)
Big MontyWeb Ninja at largeCommented:
hah much appreciated but unnecessary. glad I could help :)
fskilnikAuthor Commented:
Ok... thanks A LOT, really.  Kind Regards and be sure I am looking forward to hearing from you in future posts...   ;)
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I didn't see it suggested, but another option not needing the ability to refresh the page is setting the idle timeout in iis.  This is found under the App Pool.

https://technet.microsoft.com/en-us/library/cc771956%28v=ws.10%29.aspx

To use the UI

Open IIS Manager. For information about opening IIS Manager, see Open IIS Manager (IIS 7).
In the Connections pane, expand the server node and click Application Pools.
On the Application Pools page, select the application pool for which you want to specify idle time-out settings, and then click Advanced Settings in the Actions pane.
In the Idle Time-out (minutes) box, type a number of minutes, and then click OK.
Big MontyWeb Ninja at largeCommented:
That would be an option if the page didn't have any conditions on which it needed to reload. According to the OP,  there are some pages where that need to refresh and some that don't
fskilnikAuthor Commented:
Hello, Scott Fell and Big Monty!

Sorry for the delay, but I simply did not enter Experts Exchange since my last interaction.

Although Big Monty is right (the refresh task is needed in some pages), I thank you too, Scott Fell, on your good advice on how to deal with the idle period of time.

Thank you and kind Regards to both of you!
fskilnik.
fskilnikAuthor Commented:
I have opened another EE question VERY related to this one... if you could have a look, it would be great.

It is here: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28640836.html

Thanks A LOT!
fskilnik.
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I have gone this route you are trying a long time ago.  It is not a very good solution and just causes other issues like a domino effect as you are finding out with your next question.    

A much better solution for a log in is to use a cookie that expires in x minutes or days.

1) Log in form requesting user name and pass

2) ASP code that looks up the user record by the user name (should be unique)

3) If you have the user record, test for a good password.  

4) If good password, A) set encrypted cookie token  B) Update the token to log in table in the db with the expiration date and user info.  

5) Each page load  check the cookie token to look up the user and if the token is still valid.  

The token should be encrypted or just some type of unique hash.  On some of my password sites will add a continuous timer that is based on expiration date of the token found in the log in table.   I set the expiration with asp code, then use momentjs http://momentjs.com/ to show a continuous hrs:mins:sec until time out.    You can set up your javascript to send a modal x minutes before time out asking to extend the log in.  If yes, you will just update the log in table with a new expiration time stamp or issue a new token.

This may be over simplified but the idea is not to rely on the session.   By updating the idle timeout, you eliminate the time people take staring at the screen like a zombie or go take a phone call and come back to your system.    It is still a bit of a work around.  

When you simply do an ajax post to another page you will prevent the session from timing out but then you have to have something that eventually logs them out so they do not stay logged in forever.
fskilnikAuthor Commented:
Hello again, Scott Fell.

Sorry for the delay (here more than there)!

After the student log in the site, a certain session variable ID (unique) is with him along his session (already), therefore all actions are already related to him during his "study" (opening questions, answering others, doing mocks, etc).

The problem with stay logged in forever is EXACTLY what is going on NOW, and the updating the idle timeout would be what your other suggestion would take us (if I understood correctly), therefore I AM very interested in the token idea... could you please explain to me a bit more, perhaps with a link with same examples?

Thanks A LOT!

Kind Regards,
fskilnik.

P.S.: if I am not mistaking, YOU were the guy who created "the" javascript code for me that started this whole thing. Thanks a lot because, till now, it has been very very useful. Let´s see if we are able to make it "retire" in the near future... :)

I will open another EE post asking for token ideas... I will be back here soon to give you the link. (I guess that´s fair, because it IS another question.)
fskilnikAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.