Cordova - window.history.back()不工作在iOS 9中的HTML后退按钮 [英] Cordova - window.history.back() not working on HTML back button in iOS 9
问题描述
在我的应用程序中,我使用 window.history.back 导航回上一个视图
/ strong>
< div class =back_iconid =verification_back_icon>< a href =# data-rel =backdata-transition =slidedown>< img src =images / back_btn.pngwidth =23/>< / a>< / div>
按钮操作:
$ b b$(#verification_back_icon)。on(click,function(e)
{
if(checkDirtyVacation())
{
e.preventDefault();
if(backbtnAlt == false)
{
backbtnAlt = true;
confirm想要继续?,
function(r){
if(r){
// onBackKeyDown();
clearVacationvalues();
window.history。 back(); //这在iOS中不起作用9
} else {
}
backbtnAlt = false;
});
}
}
else
{
e.preventDefault();
if($(。vaction_location)。hasClass(chkSelect))
{
$(。vaction_location)。removeClass(chkSelect);
$(vaction_location)。addClass(chkUnSelect);
}
$ b b
window.history.back();
}
});
在iOS 9中,此导航不工作。
我使用
Apache Cordova原生平台版本3.8.0
/ p>
如果任何人面临类似的问题,请建议我。
我尝试过 history.back在iOS上使用Cordova无法运行,但没有运气
谢谢。
解决方案解决方案:
此行解决了我的问题:
history.go(0);
我替换了
window.history.back()
history.go(0);
现在它在iOS 9中正常工作
在index.html 中
script type =text / javascript> $。mobile.hashListeningEnabled = false;< / script>
在onDeviceReady函数中添加:
function onDeviceReady()
{
if(device.platform ===iOS&& parseInt(device.version) === 9){
$ .mobile.hashListeningEnabled = false;
}
if(!($ .mobile.hashListening Enabled&
$ .mobile.path.isHashValid(location.hash)&&
($(hashPage).is(:jqmData(role ='page'))||
$ .mobile.path.isPath(hash)||
hash === $ .mobile。 dialogHashKey))){
//确保设置初始popstate状态(如果存在)
//以便导航回到初始页面正常工作
if($ .event .special.navigate.isPushStateEnabled()){
$ .mobile.navigate.navigator.squash(path.parseLocation()。href);
}
$ .mobile.changePage($ .mobile.firstPage,{
transition:none,
reverse:true,
changeHash: false,
fromHashChange:true
});
} else {
//触发hashchange或导航到squash并记录正确的
//历史记录条目的初始散列路径
if(!$。event.special.navigate .isPushStateEnabled()){
$ window.trigger(hashchange,[true]);
} else {
// TODO了解如何简化与初始历史记录条目的交互
//在底部js / navigate / navigate.js
$ .mobile。 navigate.history.stack = [];
$ .mobile.navigate($ .mobile.path.isPath(location.hash)?location.hash:location.href);
}
}
验证设备操作系统版本(
history.go(0)
只适用于iOS 9)在iOS 9版本之前window.history.back()
/ p>
现在添加这段代码代替window.history.back()
if(device.platform ===iOS& parseInt(device.version)=== 9){
console.log(version + device.version);
console.log(iOS 9);
history.go(0);
//在这里写你的代码
}
else {
window.history.back();
}
要修复此邮件无法加载包含错误的网页:CDVWebViewDelegate:Navigation started在控制台中添加以下代码 CDVWebViewDelegate.m
(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
方法注释这段代码如下所示:
/ * if([_delegate respondingToSelector:@selector(webView:didFailLoadWithError :)]){
NSDictionary * errorDictionary = @ {NSLocalizedDescriptionKey:description}
NSError * error = [[NSError alloc] initWithDomain:@CDVWebViewDelegatecode:1 userInfo:errorDictionary];
[_delegate webView:webView didFailLoadWithError:error];
} * /
In my application I am using window.history.back to navigate back to previous View
Declaration of back button
<div class="back_icon" id="verification_back_icon"><a href="#" data-rel="back" data-transition="slidedown"><img src="images/back_btn.png" width="23"/></a></div>
Button action:
$("#verification_back_icon").on("click", function(e) { if(checkDirtyVacation()) { e.preventDefault(); if(backbtnAlt== false) { backbtnAlt =true; confirm("All data will be lost. Do you want to continue?", function(r){ if(r){ //onBackKeyDown(); clearVacationvalues(); window.history.back();//this is not working in iOS 9 }else{ } backbtnAlt =false; }); } } else { e.preventDefault(); if($(".vaction_location").hasClass("chkSelect")) { $(".vaction_location").removeClass("chkSelect"); $(".vaction_location").addClass("chkUnSelect"); } window.history.back(); } });
This worked perfectly till iOS 8.4. In iOS 9 this navigation is not working.
I am using
Apache Cordova native platform version 3.8.0
.If anyone facing the similar problem please suggest me. I have tried with history.back doesn't work on iOS using Cordova, but no luck
Thank you.
解决方案SOLUTION:
This line resolved my issue :
history.go(0);
I have replaced
window.history.back()
withhistory.go(0);
Now it is working fine for me in iOS 9
In index.html
<script type="text/javascript">$.mobile.hashListeningEnabled = false;</script>
Add this in onDeviceReady function:
function onDeviceReady() { if(device.platform === "iOS" && parseInt(device.version) === 9){ $.mobile.hashListeningEnabled = false; } if ( ! ( $.mobile.hashListeningEnabled && $.mobile.path.isHashValid( location.hash ) && ( $( hashPage ).is( ":jqmData(role='page')" ) || $.mobile.path.isPath( hash ) || hash === $.mobile.dialogHashKey ) ) ) { // make sure to set initial popstate state if it exists // so that navigation back to the initial page works properly if ( $.event.special.navigate.isPushStateEnabled() ) { $.mobile.navigate.navigator.squash( path.parseLocation().href ); } $.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true }); } else { // trigger hashchange or navigate to squash and record the correct // history entry for an initial hash path if ( !$.event.special.navigate.isPushStateEnabled() ) { $window.trigger( "hashchange", [true] ); } else { // TODO figure out how to simplify this interaction with the initial history entry // at the bottom js/navigate/navigate.js $.mobile.navigate.history.stack = []; $.mobile.navigate( $.mobile.path.isPath( location.hash ) ? location.hash : location.href ); } }
Validation for device OS version (as
history.go(0)
is working only with iOS 9) Prior to iOS 9 versionwindow.history.back()
working perfectlyAnd now Add this piece of code in place of window.history.back()
if(device.platform === "iOS" && parseInt(device.version) === 9){ console.log("version" + device.version); console.log("iOS 9"); history.go(0); //write your code here } else{ window.history.back(); }
To fix this Message "Failed to load webpage with error: CDVWebViewDelegate: Navigation started when state=1" in console add below code in CDVWebViewDelegate.m
In - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
Method Comment this piece of code shown below:
/* if ([_delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) { NSDictionary* errorDictionary = @{NSLocalizedDescriptionKey : description}; NSError* error = [[NSError alloc] initWithDomain:@"CDVWebViewDelegate" code:1 userInfo:errorDictionary]; [_delegate webView:webView didFailLoadWithError:error]; }*/
这篇关于Cordova - window.history.back()不工作在iOS 9中的HTML后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!