13

I'm going to implement logout button in my PhoneGap application, which will return the user into authentication page (with cleared fields). For the button I'm using anchor with onclick event:

<script type="text/javascript">
function doLogout() {
    var backlen = history.length;
    history.go(-backlen);
    window.location.replace("index.html");
}
</script>

<a data-role="none" href="#" onclick="doLogout"></a>

but it's not works, i.e. it's just returns me to that authentication page with already filled fileds, seems like just a one step back. I'm not sure for history.clear(), because googling it gave me nothing:

<a data-role="none" href="index.html" onclick="javascript:history.clear();"></a>

Could anyone advise me where is my problem? If there is another solution to handle logout event in PhoneGap I would follow it. THanks

JAAulde
  • 19,250
  • 5
  • 52
  • 63
bofanda
  • 10,386
  • 8
  • 34
  • 57

4 Answers4

19

The call to history.go will be ignored, because you are going back one more step than there are items in the history. The length property has the number of items including the current one, so if there are for example four items in the history, you can only go back three steps.

To go back to the first step, you would subtract one from the length:

history.go(-(history.length - 1));

However, that would only take you back to your start page if you opened a new window specifically for that page. Otherwise it would take the user back to the first page that he visited using that window.


There is no clear method for the history object.

Ref: https://developer.mozilla.org/en-US/docs/DOM/window.history

The browser history belongs to the user, you can't remove anything from it.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ah, but what happens when a user clicks back and you run this code? `history.length` doesn't change when Back is clicked, but running that code will overshoot the history. It seems that there's an internal pointer that we can't get the value of :( – Ja͢ck May 28 '13 at 02:37
  • @Jack: Good point. You don't have much control over the browser history, so there doesn't seem to be a solution that work for all cases. – Guffa May 28 '13 at 07:55
  • I've come up with something like `history.go(-history.length + 1); setTimeout(function() { location.href = '...default page...'; }, 0);` that seems to work for my particular case, but I hate having used it =/ – Ja͢ck May 28 '13 at 07:56
  • @Guffa history.go(-(history.length - 1)); is not working for IE. but its working fine in chrome. Please let me know whether i am missing anything ? – piyush pankaj Dec 10 '13 at 14:24
  • @piyush: I tried it in IE, and it seems to work fine. You might have some conflict with some other code, try the more specific `window.history.go(-(window.history.length - 1));`. – Guffa Dec 10 '13 at 17:18
8

I have never developed any PhoneGap, but in web applications you can use code below.

try window.location.replace(url);

after redirection there no way to go previous page.

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
KnowGe
  • 305
  • 3
  • 10
  • I knew this already.. but today i tried it on an anchor element with a click event in jquery.. its redirecting to a new page and I am able to come back to the previous page... can u help me pls..? – Thameem Apr 07 '21 at 14:38
1

If you use the onclick attribute, what goes in the quotes is an actual statement, not just the name of a function. So:

<a data-role="none" href="#" onclick="doLogout();"></a>
<!-- Note the (); ----------------------------^^^   -->

Also note that because you're not doing anything to prevent the default action of following the link, the browser will follow the href, which being # takes you to the top of the current page. You can prevent that (if you like) by adding a return false;:

<a data-role="none" href="#" onclick="doLogout(); return false;"></a>

I couldn't follow the second part of your question, but I'll just note that you don't need or want the javascript: on the onclick attribute in your second example. You only use that pseudo-protocol where a URI is expected (for instance, the href of an anchor). The content of onXYZ attributes is always code, not a URI.

Finally: Unless you're doing something with styling, both of those links are going to be pretty hard to click, what with being empty. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

use: navigator.app.clearHistory();