Link to home
Start Free TrialLog in
Avatar of fskilnik
fskilnikFlag for Brazil

asked on

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
SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fskilnik

ASKER

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

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...
Hi Rikin!

Good idea... but I guess I have to stop the routine at the first place...
I took out both 1. and 2. above, let us see if there is something else running the routine... just one moment, please.
No. Now it did not reload... let me ask you... item 2. could run without being called?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
Big Monty... you are THE one!!!!    I will do that now...
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
hey I'm always up for donations :) j/k of course!

glad it got resolved either way
Please think about a "wish list" in amazon.com, for instance. Near Xmas I WILL go there for sure...   :)
hah much appreciated but unnecessary. glad I could help :)
Ok... thanks A LOT, really.  Kind Regards and be sure I am looking forward to hearing from you in future posts...   ;)
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.
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
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.
I have opened another EE question VERY related to this one... if you could have a look, it would be great.

It is here: https://www.experts-exchange.com/questions/28640836/bootstrap-collapsible-panel-rule.html

Thanks A LOT!
fskilnik.
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.
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.)