troubleshooting Question

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

Avatar of lebron letchev
lebron letchev asked on
JavaScript
16 Comments1 Solution154 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Sebastian Strittmatter
Software Development

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros