0

I'm working with Moodle 2.6, but this isn't really a Moodle question. An activity is defined as an URL. That URL is just a custom-built video player. When the video ends, it goes back to the Moodle course page. That all works just fine, but the Moodle page doesn't reflect that this activity has been completed until the page is refreshed/reloaded using F5. Here is the javascript code that I've used to detect the end of the video:

    video1.addEventListener("ended", function() {
       history.back(1);
    });

I've tried using this:

    window.location.reload(history.go(-1));

in place of the history.back(1), thinking that this new line held a lot of promise, but no joy. I've tried other things as well, such as changing 'back' to 'go' (with the negative of the value), but that appears to function the same.

This seems like it should be a simple thing to do, but I'm no expert on this stuff, so I'm stumped. How do I get my browser to go back a page then reload it? I'm mainly using IE on the PC, and Safari on the iPad.

EDIT, Additional idea

When I was researching this, one suggestion was to add a unique ID at the end of the URL, such as using the time. So, in my case, I'd want to pull the URL out of the history buffer for the -1 entry and add something like ?123456 (which changes every time), then call that page. Is something like that a good idea, good coding standards, etc?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2574497
  • 3
  • 2
  • 6
  • Are you sure the 'ended' event is firing? Log it in the console to be sure (add console.log('event fired') before history.back) – bornytm Mar 02 '14 at 15:18
  • Well, it goes back, and if I take that statement out, it doesn't go back, so yes, I feel confident that it is firing. It goes back okay, it just doesn't reload the previous page. -- Bill – user2574497 Mar 02 '14 at 15:21
  • Sorry, I misread your question. Have you tried doing history.back() followed by Window.location.reload(); – bornytm Mar 02 '14 at 15:25
  • Yes, I tried that before, and tried it again just now. No joy. – user2574497 Mar 02 '14 at 15:30
  • This seems like a duplicate question. Please see an answer here: http://stackoverflow.com/a/35565785/5076414 – Sacky San Mar 02 '16 at 13:28

2 Answers2

3

The right solution would be setting up the URL you'd like to refer the user to.

If this is not possible for you, and answering your question, you can try redirecting the user to the referrer page using location.href:

location.href = document.referrer;

This will redirect the user to the page they came from, and the browser won't try to restore the previous state of the page.

Bruno Fondevila
  • 159
  • 1
  • 2
  • That didn't return back to the Moodle page where the video page was called. Instead, it gave me a directory listing. – user2574497 Mar 02 '14 at 15:33
  • Oh, and setting the URL that I'd want to go back to is not feasible, unless I pull it out of the history buffer. If I make a call to location.href with that URL from the history buffer, does it reload the page, or just used what is already cached? – user2574497 Mar 02 '14 at 15:40
  • Well, I tried that with just hard-coding (for now, during this test) the URL back to that page, and it seemed to work. I'll get it coded up correctly, and if it works, I'll mark this as "Answered". – user2574497 Mar 02 '14 at 15:43
  • Bruno-You answer was ALMOST correct. I tried to use that statement, and it didn't work. I went back and investigated, and found out that document.referrer didn't have a value at that point. I saved that value in my onload event (as myReferrer) and then used that value in the location.href, and that worked. Thanks so much for the right answer. I just didn't follow through with it (and paid the price). – user2574497 Mar 03 '14 at 22:05
  • Thank you, I didn't know it wasn't filled until the dom was fully loaded, I'll think about it next time I have to use it. – Bruno Fondevila Mar 05 '14 at 13:40
0

Maybe try Window.history.back() rather than just history.

bornytm
  • 793
  • 1
  • 11
  • 27
  • Replacing "'history.back(1)" with "windows.history.back(1)" results in it NOT returning to the previous page. Likewise, changing "window.location.reload(history.go(-1))" to "window.location.reload(windows.history.go(-1))" is NOT returning to the previous page. Returning to the previous page is not the problem here, as that part of it works just fine. It just doesn't refresh, so F5 is required by the user. – user2574497 Mar 02 '14 at 15:27