WebView Back Button not working
I have a simple webview loaded into a tab in an iPhone app. I load a URL initially which is an .html file with really basic HTML and standard anchor links to another local .html file (all contained in the Resources folder). The pages load file, but after I click a link I can't get the webview to go back in the javascript history using the webview goBack() method. I've tried several approaches and even the canGoBack() method of the webview returns false no matter what I do. Please see the code below. Any thoughts?
I'm using 1.6.2 SDK
var win = Titanium.UI.currentWindow;
var webview = Ti.UI.createWebView({
url: '../html/' + this_path
});
win.add(webview);
var back = Titanium.UI.createButton({
backgroundColor: '#006438',
borderColor: '#006438',
borderRadius: 6,
color: '#ffffff',
borderWidth: '2',
height: 25,
font:{size:9, fontWeight:'bold'},
width: 50,
backgroundImage: 'none',
title:'Back'
});
back.addEventListener('click', function(e){
if (webview.canGoBack()) {
webview.goBack();
} else {
win.close();
}
});
win.leftNavButton = back;
4 Answers
-
Hm. Have the same problem. Any solutions?
-
Hmm, I can't answer your question unfortunately but it is similar to a problem
I'm experiencing along the same lines.Within my HTML page I have a link with a bit of js attached to call history.back(), but this just doesn't do anything when clicked. I had thought it might be the webview handling page history, however it looks like i might end up facing the same problem as you if history doesn't actually work. To complicate the matter further I'm using jquerymobile for dynamic page transitions. Bah.
-
hey zack. Take a look at this http://developer.appcelerator.com/question/119971/confused-between-heavy-weight-vs-light-weight
I managed to create a back button on my webview window. But I'm still having difficulty with my app crashing. Not sure what it is though.
Just remember to make the window with the webview a heavy window i.e modul: true, fullscreen: true…etc…
hope this helps -
The problem I was having was somewhat similar. If I loaded a web page in a WebView via WebView.setURL, that page would not be in the history accessed by goForward/goBack, etc. However, if users then clicked on other links within the WebView, the links they clicked on manually WOULD be in the history.
Just like Zach, I ended up implementing my own history by listening for WebView's 'load' event (and filtering out those load events triggered by people clicking on the Forward and Back buttons). Frustrating, but that was the only way to get the history behavior users expected.
I am using Titanium SDK 1.8.0.1 and iPhone SDK 4.3.