Utilizing Cordova and jQuery-mobile to launch links in a separate device browser

My app has numerous links, all of which I have added rel='external' target='_blank' to. While this works perfectly in the Ripple emulator and regular desktop browsers, it doesn't function correctly on my Android device running JB 4.2.2. The link opens in the same window, and when I hit "back," it messes up the app's functionality until I physically reload it.

How can I make sure that a link opens in the device's browser instead? Do I need to utilize a Cordova plugin for this?

(The versions I am currently using are Cordova 2.9.0, jQuery 1.10.1, jQuery Mobile 1.3.1)

Answer №1

Recent updates with Cordova/PhoneGap have been quite inconsistent. It seems that the changes made for the InAppBrowser functionality may offer a solution.

One method we have found effective for launching content in an external browser is:

window.open("http://myurl.com", '_system');

In our particular case, we needed to identify external links and open them in Safari/Chrome while keeping internal links within our Angular router. While our solution may not be the most elegant, we achieve this by intercepting input events on links and modifying the behavior as shown below:

        $(document).on('mousedown','a', function(e) {
            e.preventDefault();
            var elem = $(this);
            var url = elem.attr('href');
            if (url.indexOf('http://') !== -1) {
                window.open(url, '_system');
            }
        });

Hopefully this information proves helpful to you.

Answer №2

After encountering a persistent issue with Jason Farnsworth's solution still triggering the default action upon user's return to the iOS app, I decided to make a few adjustments to the code. The revised code provided below worked as intended.

$(document).on('click', 'a', function (e) {
   var elem = $(this);
   var url = elem.attr('href');
   if (url.indexOf('http://') !== -1) {
       e.preventDefault();
       window.open(url, '_system');
       return false;
   }
});

Answer №3

There are numerous queries similar to this, all attempting to utilize the inappbrowser. To address this, I opted to utilize the following plugin:

com.byhook.cordova.chromelauncher

To integrate it, simply execute the following command in the command line:

cordova plugin add com.byhook.cordova.chromelauncher

Additionally, include the subsequent snippet of JavaScript code:

ChromeLauncher.open(url)

By implementing this, the following outcomes are achieved:

  1. Puts your application in background mode
  2. Launches the pre-existing instance of the "Google Chrome for Android" browser (in the absence of Chrome, it will redirect to Google Play - although this has not been personally verified)
  3. Creates a new tab in Chrome that navigates to the specified URL

Answer №4

After tirelessly searching for the solution, I managed to find a workaround different from the ones already provided.

It seems that using older versions of Cordova with newer versions of Android can cause issues. Updating Cordova to the latest version may introduce some migration problems in your current project, but it's definitely worth a try. I upgraded from Cordova 2.8 to 5.0, which only required a few code tweaks and took around half an hour. After rebuilding, deploying, and launching successfully, I noticed that the back button, which had caused my app to crash on older Cordova versions, now functioned perfectly with the same line of code. In short, updating Cordova, making necessary code adjustments, and rebuilding your project can resolve many issues.

I hope this information prevents others from experiencing the same struggles I faced.

Answer №5

One simple solution is to eliminate the "target" attribute entirely.

Instead, rely solely on the "rel" attribute for your needs.

I have encountered this same issue multiple times, so hopefully this solution will work for you as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React re-rendering only when arrays are filtered, and not when new elements are added

I need help with a table simulation where I can add and remove products. The issue I'm facing is that the component only updates when an item is removed, not when a new row is added. Currently, I am utilizing the useState hook. Below is my array data ...

Changing the color of an Angular <div> element with scripting

I am currently working on an Angular 6 project and I need to change the colors of a div tag from a script after a click function. However, I also need to change it back to transparent. When I click on the "Inheritance" option, the background color of the ...

How can I convert duplicate code into a function in JavaScript?

I have successfully bound values to a view in my code, but I am concerned about the duplicate nested forEach loops that are currently present. I anticipate that Sonarcube will flag this as redundant code. Can anyone advise me on how to refactor this to avo ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

Determining the width of a window using JavaScript

My website is experiencing some mysterious issues with $(window).width(). When I open my site in the Chrome Device Toolbar with a window size of 320xXXX, then run $(window).width() in Google Chrome's JavaScript console, it returns 980. As a result, n ...

I'm having trouble getting Stripe checkout to function properly with Angular 1.x

Utilizing the Stripe payment system for processing payments, I referenced a project on GitHub and a helpful blog. Incorporating nested views and routers in my project, the structure appears as follows: src app views controllers directives ...

Is there a method to retrieve a value from a node.js server when a div is clicked?

This is the EJS file I've created: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Sart Plug</title> <script src="http://code.jquer ...

Extracting data from a JavaScript React webpage with Python Selenium, targeting an element that disappears shortly after loading

Having trouble with scraping a webpage that features a React element hiding a dropdown after a few seconds. Upon initially arriving at the page, this is what is visible and what I aim to scrape: https://i.sstatic.net/VVY4r.jpg The specific information I ...

Adjust the variable value if the "for" loop encounters an error

In my situation, I have a spreadsheet that contains a script responsible for checking another linked spreadsheet for a customer's name and then returning the associated code. Everything works smoothly when the customer name is found in the "CustomerCo ...

Are the functionalities of twilio-common.js on github equivalent to those of twilio-client.js on their CDN?

Currently, I am integrating the Twilio SDK client from the twilio CDN using this link: //media.twiliocdn.com/sdk/js/client/v1.4/twilio.min.js However, I am interested in importing the package via npm due to some restrictions. The only option I see availa ...

What is the best way to retrieve the value from a textarea and verify if it is blank or not?

I have successfully incorporated a textarea using simpleMde, but I am facing difficulty in retrieving the value and checking whether it is empty or not. var message = document.getElementById("simpleMde").value; console.log(message); <textarea class=" ...

What is the best method to securely install a private Git repository using Yarn, utilizing access tokens without the need to hardcode them

My initial thought was to utilize .npmrc for v1 or .yarnrc.yml for v2/3/4, but in all scenarios, Yarn does not even attempt to authenticate with Github. nodeLinker: node-modules npmScopes: packagescope: npmAlwaysAuth: true npmAuthToken: my_perso ...

Generating fresh objects to establish a Many-to-Many connection with Sequelize

I am currently utilizing NodeJS, Express, Sequelize, and PostgreSQL in my project. Within my application, I have defined two models: a "User" model and a "Social" model. My goal is to establish a many-to-many relationship where a user can be associated wi ...

Spinning an object smoothly in the opposite direction of the OrbitControls camera's y rotation in three.js

Is there a way to use OrbitControls in three.js to make a planeMesh always remain facing the camera? I've attempted some code but it's not quite working as expected. I want the planeMesh to slowly readjust its orientation only when the camera mov ...

I'm currently working on incorporating a rating system into my Vue.js project, but I am struggling to successfully pass the rating values

After carefully reviewing the documentation, I implemented the code below. While I am successfully retrieving data for enquiryDesc, I am encountering null values for rating. Despite trying various troubleshooting steps, the issue with null values persists. ...

Using the for-each loop in Express.js with Node

I'm currently working on developing a REST API using express with Node. I have a requirement to iterate through a for loop in order to generate the desired JSON output. Here is a snippet of my route file: var Redis = require('ioredis') var ...

Optimal method for parsing URLs using ES6

There have been many remarkable inquiries regarding this topic, such as: how to parse a url. However, as time has gone by, the answers I come across are outdated. I am looking for a more modern and flexible method to parse URLs, without relying on regular ...

What is the purpose behind enclosing a function expression in parentheses in JavaScript (apart from creating an IIFE)?

While delving into some advanced JavaScript concepts, I stumbled upon a piece of jQuery code on GitHub that caught my eye. It was an anonymous function wrapped in parentheses, which you can find here. Although I'm familiar with Immediately Invoked Fu ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

Increasing space at the top with heading

As I scroll down, the header on my website remains in a static position and disappears. However, when I scroll back up, the header reappears wherever the user is on the page. While this functionality works well, I have noticed that as I scroll all the way ...