0

I am new ish to jQuery and have been trying to figure things out myself but I am being left with some questions and would like clarification when it comes to the problems I am facing. I am trying to test if a page is currently active using jQuery. At the moment, I have a page with a header and footer that stays in place and another page nested into the body portion of the page. So in other words, I have a page nested in a page.

Right now, I have the following code to test if a page is still active:

    $(document).on("click mousemove keydown", function () {
        activity = true;
    });

I have other code later on that takes this information and sends a heartbeat to keep the page alive. My problem is this: Right now this only tells me if the user is moving around or clicking in the header or footer and not in the partial that is in the body.

I found another stackoverflow page(link at bottom) that worked with nested $(document).ready() but I have questions about if this would work in my case. Does this nested $(document).ready() actually read the item below it or it just tells us a second time that the same page is ready? And if it does tell us if the inner page is ready, in my example, would I just need to wrap it in one $(document).ready() since I use the $(document).on() to test the inner page?

Difference between nested $(document).ready() and $(window).load() events

Edit:

I ended up putting the check for activity on the individual pages before using the page as the IFrame. It was a huge work-around but it ended up working as I needed it to.

The only other option I had found that would sorta work was to check if the user went into the IFrame (by checking what was blurred) and if the mouse didn't move positions on the page for a set amount of time, that is when the user was considered inactive. This wasn't enough in my case so I couldn't just do this option.

KR8T3
  • 7
  • 6
  • `$(document).ready()` is based on an event that happens only once per page load. What do you mean by inner page? An iframe? – zer00ne Jun 19 '19 at 12:40
  • So I have an outer page (header & footer) that stays the same as the user mvoes around on another page in the body. I believe this would be an IFrame. If I used $(document).ready() to test if the full page has loaded then used $(document).on() inside of that, would that allow me to test for user activity on the IFrame? – KR8T3 Jun 19 '19 at 12:44
  • iFrames are really difficult to monitor and almost impossible if you don't have control of the page within the iframe -- as in you don't have admin privileges to edit said page. – zer00ne Jun 19 '19 at 12:53
  • Then maybe it isn't an IFrame. I just started working on this code and I'm still trying to learn what is going on with it.Either way, the inner page is a fully functional page that can be interacted with and edited. I'll keep trying to figure out something with this though. – KR8T3 Jun 19 '19 at 12:58
  • would using $(window).on("click") test to see if anywhere on the full loaded page was clicked? – KR8T3 Jun 19 '19 at 13:31
  • I'll need actual HTML to see what's possible, *"inner page"* is too vague. Please read on how to post a [mcve] – zer00ne Jun 19 '19 at 13:35
  • https://jsfiddle.net/nmyx27kj/ This is a very similar layout to what I have - I cannot actually share any of my code - but this is set up the same way. Right now, if you click anywhere in the white space, it gives the alert. I need it so that if you click in the black box, it will give the alert there. I was able to figure out the partial page I was referring to is an IFrame – KR8T3 Jun 19 '19 at 14:15
  • Does the parent page (the *"outer page"* with the header and footer) belong to the same domain as the child page (the *"inner page"* inside the iframe)? Example: parent page is located at: `https://www.example.com/path/to/parent_page.html` and child page is located at: `https://www.example.com/path/to_the/child_page.html` – zer00ne Jun 19 '19 at 14:55
  • yeah they both belong to the same domain – KR8T3 Jun 19 '19 at 15:16

0 Answers0