4

I want to convert current url to https version with javascript. How can i done this ?

Ex: http://www.example.com --> https://www.example.com

2 Answers 2

15
window.location = window.location.href.replace(/^http:/, 'https:');
Sign up to request clarification or add additional context in comments.

1 Comment

Good one! You probably also want to check whether it is http: before: if (window.location.protocol == 'http:') {}
4

If you know that your http URL starts with "http://", then it is as simple as:

var httpsUrl = "https" + httpUrl.substring(4);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.