Javascript 是否可以 onclick="history.clear();"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14920806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to onclick="history.clear();"
提问by Dozent
I'm going to implement logoutbutton 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:
我将在我的 PhoneGap 应用程序中实现注销按钮,这将使用户返回到身份验证页面(带有清除的字段)。对于按钮,我使用带有 onclick 事件的锚点:
<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:
但它不起作用,即它只是将我返回到已填写文件的身份验证页面,似乎只是后退了一步。我不确定history.clear(),因为谷歌搜索它没有给我什么:
<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
谁能告诉我我的问题在哪里?如果有另一种解决方案来处理 PhoneGap 中的注销事件,我会遵循它。谢谢
回答by Guffa
The call to history.gowill be ignored, because you are going back one more step than there are items in the history. The lengthproperty 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.
调用history.go将被忽略,因为您比历史记录中的项目多返回一步。该length属性具有包括当前项目在内的项目数,因此如果历史记录中有四个项目,则您只能返回三步。
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 clearmethod for the historyobject.
对象没有clear方法history。
Ref: https://developer.mozilla.org/en-US/docs/DOM/window.history
参考:https: //developer.mozilla.org/en-US/docs/DOM/window.history
The browser history belongs to the user, you can't remove anything from it.
浏览器历史记录属于用户,您无法从中删除任何内容。
回答by KnowGe
I have never developed any PhoneGap, but in web applications you can use code below.
我从未开发过任何 PhoneGap,但在 Web 应用程序中,您可以使用下面的代码。
try window.location.replace(url);
尝试 window.location.replace(url);
after redirection there no way to go previous page.
重定向后无法转到上一页。
回答by T.J. Crowder
If you use the onclickattribute, what goes in the quotes is an actual statement, not just the name of a function. So:
如果使用该onclick属性,引号中的内容是实际语句,而不仅仅是函数名称。所以:
<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;:
另请注意,由于您没有采取任何措施来阻止跟随链接的默认操作,因此浏览器将跟随href,这会将#您带到当前页面的顶部。您可以通过添加以下内容来防止(如果您愿意)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 onclickattribute in your second example. You only use that pseudo-protocol where a URI is expected (for instance, the hrefof an anchor). The content of onXYZattributes is always code, not a URI.
我无法理解您问题的第二部分,但我会注意到您不需要或不想要第二个示例中的javascript:ononclick属性。您只在需要 URI 的地方使用该伪协议(例如,href锚点的 )。onXYZ属性的内容始终是代码,而不是 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. :-)
最后:除非你正在做一些样式的事情,否则这两个链接都将很难点击,什么是空的。:-)
回答by adsdsadsdasdasd
use: navigator.app.clearHistory();
使用:navigator.app.clearHistory();

