Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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;
— asked May 18th 2011 by Zach Hendershot
  • back
  • button
  • iphone
  • webview
3 Comments
  • I found out this same code works with a remote URL, like http://www.google.com.

    — commented May 18th 2011 by Zach Hendershot
  • I ultimately solved this problem myself by building my own history stack of HTML files and handle the back button functionality within the app.

    — commented May 19th 2011 by Zach Hendershot
  • This problem exists even in 3.0.2. Hi Zach, would you mind sharing your implementation of the "history" feature?

    — commented March 29th 2013 by Gopi Reddy

4 Answers

  • Hm. Have the same problem. Any solutions?

    — answered November 10th 2011 by Ievgen Kyts
    permalink
    0 Comments
  • 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.

    — answered May 20th 2011 by Lee Sibbald
    permalink
    0 Comments
  • 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

    — answered May 26th 2011 by David Craigster
    permalink
    0 Comments
  • 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.

    — answered January 2nd 2012 by William Drury
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.