Search code examples
javascriptcordovalogout

Is it possible to onclick="history.clear();"


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


Solution

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