Redirect users to specific browsers on Android (using Intents) and iOS (using URL schemes).
- Features
- Supported Browsers
- Installation
- Usage
- Live Demo
- How It Works (Technical Deep Dive)
- API Reference
- Support This Project
- Got Issues?
- License (MIT)
- π TypeScript support (Fully typed, tree-shakable, and framework-agnostic).
- π Platform detection (Android / iOS).
- π§ In-App browser detection (Instagram, Facebook, TikTok, etc.).
- π Redirects via URL schemes or Android intents.
- β 10+ supported browsers.
| Browser | Android Package Name | iOS URL Scheme |
|---|---|---|
Google Chrome |
com.android.chrome | googlechrome:// |
Mozilla Firefox |
org.mozilla.firefox | firefox://open-url?url= |
Brave Browser |
com.brave.browser | brave://open-url?url= |
Microsoft Edge |
com.microsoft.emmx | microsoft-edge-https:// |
Opera Browser |
com.opera.browser | touch-https:// |
Opera GX |
com.opera.gx | opera-gx://open-url?url= |
DuckDuckGo Browser |
com.duckduckgo.mobile.android | ddgQuickLink:// |
Samsung Browser |
com.sec.android.app.sbrowser | - |
Vivaldi Browser |
com.vivaldi.browser | vivaldi:// |
UC Browser |
com.UCMobile.intl | - |
Yandex Browser |
com.yandex.browser | - |
Safari |
- | x-safari-https:// |
yarn add browser-switchernpm install browser-switcherYou can use browser-switcher directly in the browser without installing any package. There are two main options:
1. IIFE version:
<script src="https://unpkg.com/browser-switcher"></script>- This is the minified IIFE version (
dist/index.min.js) suitable for including via a standard<script>tag. - The
BrowserSwitcherclass is exposed as a global variable, no import required.
<script type="text/javascript">
const link = document.getElementById('link');
link.addEventListener('click', (e) => {
e.preventDefault();
try {
BrowserSwitcher.default.open({
targetUrl: 'https://google.com',
platform: 'auto',
browser: 'chrome',
});
} catch (error) {
console.log(error);
}
});
console.log(BrowserSwitcher.SUPPORTED_BROWSERS);
</script>2. ES Module:
<script src="https://unpkg.com/browser-switcher/dist/index.js" type="module"></script>- This is the ES module version (
index.js) for modern browsers supportingimport/export. - You must use
type="module"in the<script>tag.
<script type="module">
import BrowserSwitcher, { SUPPORTED_BROWSERS } from 'https://unpkg.com/browser-switcher/dist/index.js';
BrowserSwitcher.default.open({
targetUrl: 'https://google.com',
platform: 'auto',
browser: 'chrome',
});
</script>If you want a previous version, check the instructions at https://unpkg.com.
import BrowserSwitcher from 'browser-switcher';
try {
BrowserSwitcher.open({
targetUrl: 'https://example.com',
platform: 'auto',
browser: 'chrome',
method: 'href'
});
} catch (error) {
console.log(error);
}
const currentPlatform = BrowserSwitcher.detectPlatform();
console.log(currentPlatform);You can explore a fully interactive demo of the package here: browser-switcher-demo.jakkimcfly.com
The demo includes clickable browser icons, so you can test how redirection works for each supported browser on your device.
You can also find the example code in the
exampledirectory of this package.
This library uses platform-specific mechanisms to launch URLs in external browsers or apps. Here's how redirection works under the hood for each platform:
-
Mechanism: Uses Android intents to open URLs in a specific browser.
-
Format:
intent://<host>/<path>#Intent;scheme=https;package=<browser-package>;end;
-
Mechanism: Uses custom URL schemes registered by each browser app (e.g.,
googlechrome://). -
Format:
googlechrome://www.example.com -
Limitations: Only works if the corresponding browser app is installed.
Android:
- Android intents may show an app chooser.
- If the specified browser is not installed, the system may either open its
Google Playpage for download or simply fail to perform the redirect (e.g., in some browsers like DuckDuckGo Browser or Mozilla Firefox).
iOS:
- iOS always shows an alert before switching apps.
Redirects the user to a specific browser using platform-specific deep linking (Android intents, iOS URL schemes, or standard links).
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
targetUrl |
string | β Yes | The URL to open. |
platform |
Platform | 'auto' | β Yes | Platform to target. If 'auto', it will be detected automatically. |
browser |
SupportedBrowserId | β No | Target browser ID (e.g., 'chrome', 'firefox', 'safari'). |
method |
OpenMethod | β No | Redirect method: 'href' (default), 'replace' or 'open'. |
windowOptions |
OpenWindowOptions | β No | Only used with 'open' method to customize window. |
Important:
If the browser parameter is not specified:
- On iOS, the link will be opened in Safari by default.
- On Android, the
intent://scheme will be used without specifying a package name, allowing the system to choose the appropriate application to handle the link.
Recommendation:
- Wrap the call in a
try...catchblock.
If you specify a browser that is not supported on the selected platform, the method will throw an error (
throw new Error). This allows you to handle the case gracefully instead of letting it break execution.
Opening Methods:
This package supports three strategies to open a link. Each has different browser behaviors and use cases:
href- Standard navigation, user can go back.replace- Redirects where "Back" button should not return.open- Open in new tab or window.
Detects the user's platform based on the navigator.userAgent.
Returns: 'android' | 'ios' | 'desktop'
Attempts to detect the currently used browser.
Returns: A BrowserInfo object or null if not recognized.
Returns configuration (Internal ID, label, Android package, iOS URL scheme) for a specific browser.
Example:
const config = BrowserSwitcher.getBrowserConfig('chrome');
console.log(config.androidPackage); // com.android.chromeReturns a list of supported browsers for a given platform (or auto-detected if not specified).
Example:
const androidBrowsers = BrowserSwitcher.supportedBrowsers('android');Detects if the user is currently inside an in-app browser (e.g., facebook, instagram, tiktok).
Returns: Name of the in-app browser or null if not detected.
Shorthand for checking if detectInAppBrowser() returns a value.
Example:
if (BrowserSwitcher.isInAppBrowser()) {
console.log('You are in an in-app browser');
}Before reporting:
- Check if someone already reported it.
- Try the latest version (
npm updateoryarn upgrade).
When reporting:
- What you expected vs what actually happened
- OS/Browser/Node versions
- Code snippet that triggers the issue
- Any error messages
Be kind β I work on this in my free time.
This is an open-source passion project. If you find it useful, hereβs how you can help keep it alive:
Code Contributions:
- Found a bug? Send a PR with a fix!
- Want a feature? Open an issue and letβs discuss.
- Improved something? Iβd love to see your improvements.
Other Ways to Help:
- β Star the repo β helps more folks discover it
- π Report bugs β even just "this broke on [X]" helps
- π¬ Share your use case β tell me how youβre using it
No pressure, but if youβre feeling generous:
This project is open-source and licensed under the MIT License - one of the most permissive licenses that gives you a lot of freedom with few restrictions.
TL;DR:
- β Use it anywhere (even commercially)
- β Modify as needed
- β No need to credit (but appreciated)
- β Donβt sue me if something breaks