Titanium Community Questions & Answer Archive

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

How can i hide a webview when the url gets a 404 error?

Title says it all id really appreciate a code example willing to pay some money if the solution works.

— asked January 21st 2012 by Matt Pilott
  • webview, hide, 404
0 Comments

6 Answers

  • Accepted Answer

    Hi, here's a working example:

    var wv = Ti.UI.createWebView({
        url:'http://developer.appcelerator.com/questionsdfd/130976'
    });
    
    var xhr = Ti.Network.createHTTPClient();
        xhr.onload = foo;
        xhr.open('GET', 'http://developer.appcelerator.com/questionsdfd/130976');
        xhr.send();
    
    var win = Ti.UI.createWindow({ title:'Tab 1', backgroundColor:'#fff' });
    win.add(wv);
    win.open();
    
    function foo() {
        if (this.status == 404) {
            alert('Got 404 error - WebView will be hidden.');
            wv.hide();
        }
    };
    
    — answered January 21st 2012 by Minh Nguyen
    permalink
    0 Comments
  • You could use an HTTPclient call to check for the existence of the page before displaying the web view. You are looking for the status value specifically.

    — answered January 21st 2012 by Stephen Feather
    permalink
    3 Comments
    • The other way is to add an error event listener to your web view. I don't recall if it throws out a reason or not for failure.

      — commented January 21st 2012 by Stephen Feather
    • scratch the second one. A 404 doesn't fire an error event.

      — commented January 21st 2012 by Stephen Feather
    • var xhr = Ti.Network.createHTTPClient();
      xhr.open("GET", 'http://www.evangelapps.com/index.php');
      xhr.onload = function(e){
          alert(this.status);
      };
      
      xhr.onerror = function(e){
          alert(this.status);
      };
      xhr.send();
      

      Returns a 200

      — commented January 21st 2012 by Stephen Feather
  • Can you give me a working example please im struggling.

    — answered January 21st 2012 by Matt Pilott
    permalink
    0 Comments
  • you could listen the webview load event and look the webview.html property for "Error 404" string on title. Not 100% valid, but can do the job in most cases.

    — answered January 21st 2012 by Javier Rayon
    permalink
    0 Comments
  • I figured a workaround but thanks for all your help

    — answered January 21st 2012 by Matt Pilott
    permalink
    1 Comment
    • You going to share your work around, or keep it all to yourself?

      — commented January 22nd 2012 by Stephen Feather
  • Essentially i wanted to use this method to point to a page that tells you if the app is up to date so instead of using titanium i did it via the website. Implementing page redirect code which i can switch off and thus the page that shows the update message will be displayed. So not used titanium at all but i have managed to get exactly what i wanted.

    — answered January 22nd 2012 by Matt Pilott
    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.