2013-05-16 19 views
1

如何使用javascript在phonegap上获取上一页?获取前一页phonegap javascript

只有当我来自一个特定的html文件时,我才想返回(window.history.back()),否则我想在按下返回按钮时转到另一个html页面。 所以我想知道哪一个是历史上的上一页。

我一直在谷歌搜索一下,我没有找到适合我的答案。

回答

9

如果你想要内置的东西,你可以使用document.referrer来获得以前的URL。但它可能不适合您的需求。更多信息here,herehere

另一个非常简单的选择是将当前页面名称存储在会话存储中,然后读取它,然后决定下一步该做什么。

//retrieve the previous page 
var previouspage = window.sessionStorage.getItem("page"): 
// if thee is no previous page you are on the first page 
if (previousPage == undefined){ 
//do something if you want, this means it's the first page the user is seeing 
} 
//get current page address 
var currentPage = window.location.href; 
//store it 
window.sessionStorage.setItem("page",currentPage); 
//check if the previous page is the one you wanted. 
if (previousPage == "the_page_you_wanted"){ 
// do something. 
} 

LocalStorage vs SessionStorage

+1

感谢,这是真正有用的,我也想过会使用localStorage来的,但我认为这是非常相似的sessionStorage的,不是吗? – Monica

+1

请勿在此处使用localstorage。 LocalStorage会在应用程序停止运行时保存数据,与SessionStorage相反,一旦您退出应用程序,它将被清除。这样可以防止用户关闭应用程序“the_page_you_wanted”时出现错误结果。 – caiocpricci2