A small utility to safely open URLs, upgrading
http
tohttps
when necessary.
This is meant to be used in browser, not in the node environment.
Modern browsers may block http://
URLs when opened from an https://
page due to mixed content restrictions. safe-open
ensure http://
URLs are automatically upgraded to https://
when the current page uses HTTPS.
Note: This requires the original URL itself to support HTTPS.
npm install safe-open
yarn add safe-open
pnpm install safe-open
import open from 'safe-open';
// Open 'https://www.google.com' if the current page is served over HTTPS
open('http://www.google.com');
// Specify target window (e.g., same tab)
open('http://www.google.com', '_self');
// Specify window features
open('http://www.google.com', 'mozillaWindow', 'popup');
This function follows the same signature as window.open.
url
(string) – The URL to open. If the page is HTTPS and the URL starts withhttp://
, it will be automatically converted tohttps://
.target
(string) – Specifies where to open the URL (_blank
,_self
, etc.).features
(string) – Additional window features such as size or position.
This project is created using generator-stupid.