Titanium Community Questions & Answer Archive

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

Possible to open all webview links in new window?

On my iPad app–

I'd like to show some html based content in a part of the screen; but I do not want hyperlinks to load within that containing webview, but instead load inside a webview in a new window that will float above. Is this possible, and if so how would one approach this?

— asked May 9th 2010 by Dan Newman
  • ipad
  • webview
0 Comments

3 Answers

  • Hi Dan,

    This would be possible with local HTML, but with HTML loaded from a remote server there would not be a way to do this since the remote page would not be able to communicate with the native wrapper. If you control the HTML being served, you can communicate with the native wrapper using the "local eval" method demonstrated in the Kitchen Sink.

    — answered May 11th 2010 by Kevin Whinnery
    permalink
    1 Comment
    • What is this "local eval" method that you are referring to? That link is sadly dead. I can only presume it's a method of using a local file, or perhaps inline HTML, to open content from an externally-served HTML? Is is as simple as using a <META> Refresh scheme, or an iFrame?

      — commented August 22nd 2011 by R. B.
  • Just copied the answer from Pastie to this post ;) (the solution works i have tested it)

    Make sure you make a reference to the jQuery library in the header of your HTML. Also remember that you must load the HTML into the WebView from a local file.

    This event definition goes in you app.js:

    Ti.App.addEventListener('openBrowser',function(e){
        Ti.Platform.openURL(e.URL);
    });
    

    This JQuery goes in the HTML file:

    $(document).ready(function(){
        $('a').each(function(){
            $(this).bind('click',function(){
                var theURL=this.getAttribute('href');
                var eventObject = new Object;
                var a = 'URL';
                eventObject[a] = theURL;
                // Fire Titanium Event to Open the URL in a new Window
                Ti.App.fireEvent('openBrowser',eventObject);
                return false;
            });                                
        })                 
    });
    
    — answered June 23rd 2011 by Hannes Kaufmann
    permalink
    2 Comments
    • Hi Hannes,

      I've implemented this solution in my app and it works perfectly in the simulator, but not on the device itself…. it still opens the links still in the web view. Any ideas what I might be doing wrong?

      Using iOS 5 on a iPhone 4S.

      Thanks
      Andy

      — commented December 6th 2011 by Andy Whitwood
    • Hi Andy,
      Try adding an alert right after you grab the theURL variable to see if it is indeed getting to that point. It sounds to me like some syntax/parse error that is not letting Ti properly fire the event.

      — commented March 22nd 2012 by Ricardo Alcocer
  • Hello. So I had to hack this a little bit, but I was able to do it and I'm sharing here.

    Opening webView link in new window : Pastie

    Another thing that I did, in order to be able to "serve" remote files is that I download the HTML, and then add a <base href> tag, specifying where it is supposed to live. That way I can show a remote HTML file from the local file system, without losing all the internal references it has, such as images and links. Caveat is that the reference to your jQuery library and any other relative reference that YOU need must be converted into an absotute reference, otherwise they will be treated as belonging to the original context of the page.

    — answered March 15th 2011 by Ricardo Alcocer
    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.