Reputation: 1757
In my javascript application, my users can input an external link/URL somewhere in the admin panel. The users are not required to put https://
in the URL field.
Just like how we put somewebsite.com
in browser's URL bar and the browser automatically chooses https
or http
(whichever is available), the app should automatically choose https://somewebsite.com
or http://somewebsite.com
.
Currently, I have something like this in my app:
var url = 'www.csszengarden.com'; // input from user
window.open('//'+url);
The problem here is it automatically resolves the website to HTTPS even though the website is on HTTP.
How can I open an URL (without HTTP or HTTPS) and then automatically choose whichever is available?
Upvotes: 0
Views: 2261
Reputation: 28870
The usual way to do this when you don't know which protocol the site supports is to use http
and let the site redirect to https
.
But if the user does provide a protocol, you want to keep that.
So a simple solution could be:
if( url.indexOf('//') < 0 ) {
url = 'http://' + url;
}
You can probably improve on that, but it may get you started.
Upvotes: 2