Skip to content Skip to sidebar Skip to footer

Opening An Url In A New Browser Window In Android Working With Phonegap

I've working on an app, that takes data from a RSS feed (xml), this means I don't have control of what comes with it, I'm using the Google Api for feeds to retrieve the data in JSO

Solution 1:

After researching a lot here and google, I came up with the following.

Along with the content displayed in my app, there is a back button, with an onClick event, I used this button to define a function and call the event targeting the link inside the article. By the way the button had a # href attribute.

After doing this, I added the onClick event via "attr", and later left the href attribute with a #.

The script goes as follows:

functionbutton(){
        var button = $(location).attr('href');
        button = button+'#';
        $('#articles a').each(function(){

              if(this != button){
                    $(this).attr("onClick", "navigator.app.loadUrl('"+this+"', { openExternal:true });");
                    $(this).attr("href", "#");  
                }
            });
    }

Inside the onClick, I called the event for opening the new browser window, which in my version of phonegap was "navigator.app.loadUrl"

Now every time the html loads the RSS feed content, and it comes with some link inside it display this:

< a onclick="navigator.app.loadUrl('https://google.com/', { openExternal:true });">Link< /a>

This opens a dialog box, where the user can pick the browser he wants to use.

Thanks again to the people taking the trouble to answering my question!

Solution 2:

If you use Jquery you can use this function :

<script> 
   $(document).on('click', 'a', function(e) {
      if ($(this).attr('target') !== '_blank') {
         e.preventDefault();
         window.location = $(this).attr('href');
      } 
   });
</script>

Be sure to add rel="external" in the <a> tag if you use also jQuery Mobile within your app. Hope it helps, ask if something is not working. Happy coding!

Post a Comment for "Opening An Url In A New Browser Window In Android Working With Phonegap"