This content originally appeared on DEV Community and was authored by Aleksandr Zakharov
Seamlessly open links in Chrome, Firefox, Brave, Edge, Opera, Samsung Internet, Vivaldi, UC, Safari and more β with Android Intents and iOS URL Schemes.
Live demo: https://browser-switcher-demo.jakkimcfly.com
GitHub: https://github.com/jakkimcfly/browser-switcher
npm: https://www.npmjs.com/package/browser-switcher
Have you ever needed to redirect users from one browser to another? For example, when you want a link to be opened directly in Chrome, Firefox, or another browser instead of the current one?
Thatβs exactly what my new npm package browser-switcher does. It helps you:
- Seamlessly redirect users.
- Fully typed (TypeScript).
- Detect the current browser.
- Supported 10+ browsers.
- Detect In-App browser (e.g., Facebook, Instagram, TikTok).
In this post, Iβll walk you through the basics of using browser-switcher
, give you a working example, and briefly cover its core methods.
Installation
You can install the package via npm or yarn:
npm install browser-switcher
# or
yarn add browser-switcher
Basic Usage Example
Imagine you want to force open a link in Google Chrome when the user clicks a button:
import BrowserSwitcher from 'browser-switcher';
document.getElementById('open-chrome')?.addEventListener('click', () => {
try {
BrowserSwitcher.open({
targetUrl: 'https://example.com',
platform: 'auto',
browser: 'chrome',
});
} catch (error) {
console.error('Browser switching failed:', error);
}
});
On Android, this will use
intent://
to launch Chrome.
On iOS, it will use the Chrome URL scheme (
googlechrome://
).
If the browser is not installed, behavior depends on the browser β some may redirect to Google Play, others may just fail (e.g., DuckDuckGo, Firefox).
Full details are in the README.
Example
You can find a working demo in the example folder of the repository.
Conclusion
With browser-switcher
, you can easily:
- Detect the current browser.
- Open links in other browsers.
- Provide users with a simple UI to choose their browser.
Check it out here: GitHub Repo
Thatβs it! In just a few lines of code, you can reliably switch users between browsers on both Android and iOS.
This content originally appeared on DEV Community and was authored by Aleksandr Zakharov