0

I have requirement where I need to detect inactivity of web page and if user is inactive for more than 15 min then user should be logged out.For this purpose I am trying to detect inactivity of user by attaching click/keypress events to whole whole page and also for the main iframe.

The problem here is I am able to detect keypress / click event on the main page (main iframe) but wherever showDialoge(another iframe), I am not able to detect this event. Even though this events are being added to body of main iframe and also to whole web page, somehow i am not able to detect the same.Please help.

I am using OpenText Cordys framework XFORMs / JavaScript.

No jquery as of now but if required I can used.

Rohit Narvekar
  • 349
  • 3
  • 9

3 Answers3

1

If your files are on same server, you can call parent window's function from child iframe like

parent.html

<script>
window.whichKey = function(keycode) {
    console.log("I'm called from child frame, you pressed " + keycode);
}
</script>

child.html

<script>
window.onkeypress = function(event) {
    window.parent.window.whichKey(event.keyCode);
} 
</script>

The other possible solution for your case is passing a variable in the url of parent from your child iframe. For example window.top.location.href = "parent.html?inactive=true"; and in the parent window you can check for this variable and perform required action.

This is how you can bind an event from parent to child frame's body for listening keypress using jQuery

$("#child-frame").bind("load", function(){
    $(this).contents().find("body").on('keypress', function(e) {
        console.log(e.keyCode);
    });
});
AZee
  • 520
  • 1
  • 6
  • 22
  • This will require changes in all the child pages which I am trying to avoid, as I have already added event to main iframe which should be invoked in all of the child iframes(which is having different id's obviously). Secondly there are around 70-80 child pages which will be called from dashboard(main landing page). – Rohit Narvekar Mar 29 '17 at 03:15
  • Can you please share your code of main iframe and where you are calling it inside child frame @R23 – AZee Mar 29 '17 at 03:19
  • Its a product OpenText Cordys which will call iframe internally using its own APIs which is nothing but HTML, but dont have such direct method. This is how child ifram is called :: application.select(childBackgroundInfo.XMLDocument.documentElement.cloneNode(true),evtObj); application.showDialog(MyCases.documentElement, data, null, closeHandler, false); This two APIs are used for the opening the child pages in different iframe. – Rohit Narvekar Mar 29 '17 at 03:27
  • But why my main iframe events not working in child iframe ? Any input on that ? – Rohit Narvekar Mar 29 '17 at 03:50
  • Can't tell much from the above code. Possibly they are not getting bind to anything in the child. This is for sure that you have to bind them yourself, browser will not pass it to child itself even if code is present in the parent. @R23 – AZee Mar 29 '17 at 04:00
  • If you are sure that your events are already bound with child, then web browser could be blocking it because of these two reasons, 1. Both parent & child are on different location/domain/host. 2. They are using different protocols, one might be on `http` and other on `https` – AZee Mar 29 '17 at 04:05
0

If I understand your issue it has to do with event propagation. I'll admit though that I am less familiar with iframes. See this other post. What is event bubbling and capturing?

Aside from your question, ideally you should manage this via sessions. Expire the session after 15min. If the user is not authorized, aka not logged in, send them to the main page.

Community
  • 1
  • 1
asarenski
  • 46
  • 6
  • But in this case session will be expired after every 15 min even though user is doing something or not. If user is active on the page and timer is 15 min then also system will logged him out and redirect to login page, that is not advisable here...Please help – Rohit Narvekar Mar 29 '17 at 03:17
  • This is not true. It will expire in 15 min if they make no requests to the server. Otherwise the session timeout refreshes. This of course depends on the framework. – asarenski Mar 29 '17 at 03:33
0

system.windows will return you the window objects of the opened applications. You can attach the event handler to all windows and do it.