Add “http://” or “https://” to a URL that contain only the relative url starting by “www” or not

Dear all,

I am using the script below for reading an url and automatically add to one the "Protocol" Http or Https.  The script below reading only https or http, because I cannot add another ELSE IF. This script is good for any url starting by "www", but many urls don´t starting by "www". So I have 2 problems. Could someone help me to improve this code for adding automatically the protocol and independetly if starts or not by 'www'?


<script>
function formatURL()
{
var url = document.getElementsByName("URL")[0];
var formattedURL = document.getElementsByName("formattedURL")[0];
url = url.value;
if(url.substr(0,3) === "www") // it is for an url starting by www but I need for urls starting or not by www
{
formattedURL.value = "http://"+url;

return;
}

// I need here other alternative to https:
}
formattedURL.value = url;
}
</script>

The results are reading by a form that "convert" the url without http or https with www or without www to real url to open it in another screen

Sorry by the inconveniences

Sincerely

Letchev
lebron letchevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
What do you  mean you can't add another else if?

You don't actually have an else if in the posted code and you can have multiple else if statements
lebron letchevAuthor Commented:
Yes, Another else if does not works.

if(url.substr(0,3) === "www") // with our without www

{
formattedURL.value = "http://"+url;

return;
}

else

{
formattedURL.value = "https://"+url;

return;
}
leakim971PluritechnicianCommented:
what is the condition to use https ?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Looks like you're making this overly complex.

If you simple write links without a protocol, like //foo.com (rather than http: or https:) then likely your code will work.

Give this a try first.
Sebastian StrittmatterSoftware DevelopmentCommented:
Why you don't use an regex?

Something like this. If the match is false then you have to add the http or https

var url = "www.test.com"
if(!url.match(/^https?:\/\/.*/)) {
 url = "http://"+url
}

Open in new window


Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lebron letchevAuthor Commented:
It does not determine the correct protocol. Try with www.ebay.co.uk and www.elmundo.es
leakim971PluritechnicianCommented:
what is the condition to use https ?
lebron letchevAuthor Commented:
No special condition. The user places the domain name without http or https. The script read the url and show the full url including the protocol
Sebastian StrittmatterSoftware DevelopmentCommented:
I have tested my regex an it works but if there is no condition for https and http then you have to decide. Or do you want to call the page via https an check if there is a redirect to https and show this result to the user?
lebron letchevAuthor Commented:
Yes, redirect to http or https

For example, the user inputs: www.elmundo.es - the output would be http://www.elmundo.es
                                                     en.wikipedia.org/wiki/HTTPS - output = https://en.wikipedia.org/wiki/HTTPS
                                                      www.searchenginejournal.com - output = https://www.searchenginejournal.com

Thank you for your interest and patience.
leakim971PluritechnicianCommented:
ok you want to figure out if the site the user type redirect people to https right?
I don't think it's possible using only javascript you can detect redirect using php
lebron letchevAuthor Commented:
Wow... another sleepless night
lebron letchevAuthor Commented:
Dear all again

For reading the contents of a remote web page (Asp classic, PHP or Javascript) using  MSXML2.ServerXMLHTTP it is impossible open a remote page without place the Protocol http or https, but in general people does not writing the protocol qhen reading a remote web page through a web browser, but when we create a script for reading contents of a remote web page we need identify the procotol evey time.
leakim971PluritechnicianCommented:
you still want to do this with javascript?
lebron letchevAuthor Commented:
Yes. Javascript would be the first alternative, but I will search on google to see if I find anything in PHP (probably) or in ASP CLASSIC (better). I need a script or partial code for redirecting the url without protocol through an input box for real page

for example: INPUT: WWW.ELMUNDO.ES
                       OUTPUT: HTTP://WWW.ELMUNDO.ES

                       INPUT: en.wikipedia.org/wiki/HTTPS
                       OUTPUT: https://en.wikipedia.org/wiki/HTTPS

I
lebron letchevAuthor Commented:
Thank you.

I found the solution  by chance (literally)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.