Check if a URL is absolute
npm install is-absolute-urlimport isAbsoluteUrl from 'is-absolute-url';
isAbsoluteUrl('https://sindresorhus.com/foo/bar');
//=> true
isAbsoluteUrl('//sindresorhus.com');
//=> false
isAbsoluteUrl('foo/bar');
//=> false
isAbsoluteUrl('javascript:alert(1)');
//=> false
isAbsoluteUrl('javascript:alert(1)', {httpOnly: false});
//=> trueType: string
The URL to check.
Type: object
Type: boolean
Default: true
Only allow HTTP(S) protocols.
When set to false, any valid absolute URL will be accepted, including potentially unsafe protocols like javascript:, ftp:, ws:, etc.
Warning: Setting
httpOnlytofalsecan pose security risks as it will returntruefor URLs with protocols likejavascript:,vbscript:,data:,ftp:,ws:, etc. Only set this tofalseif you understand the implications and have appropriate safeguards in place.
See is-relative-url for the inverse.