1

I am using the function to set the https to the url passed in input.

setHttps(a) {
    if (a.indexOf('http://') > 0){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

When I pass the following:

  1. www.example.com --> converts to ---> https://www.example.com
  2. http://www.example.com --> doesn't get converted to ---> https://www.example.com

Why? It looks like indexOf('http://') is unable to find http:// in the string.

2

7 Answers 7

4

There are atleast 2 issues in your code:

a.indexOf('http://') > 0    // should be >= 0
a.replace('http', 'https')  // replace returns a String  

Try the following:

setHttps(a) {
var a = self.previewUrl.value;
if (a.indexOf('http://') >= 0){
        a = a.replace('http', 'https')
        console.log('priting url if...... ', a);
    } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
        a = a.replace (/^/,'https://');
        console.log("printing url else........ ", a);
    } 
return a;

}

Sign up to request clarification or add additional context in comments.

Comments

3

You need to update the variable with returned value. Although use http:// instead of http to avoid matching http in https and index starts from 0 so update condition based on that.

if (a.indexOf('http://') > -1){
    a = a.replace('http://', 'https://')
}

UPDATE : In the else if condition a.indexOf('http://') == -1 is not necessary since it already checked in first if.

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a= a.replace('http://', 'https://')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1) {
            a = a.replace (/^/,'https://');
            // or simply concat
            // a = 'https://' + a;
            console.log("printing url else........ ", a);
        } 
    return a;
}

2 Comments

if the variable contains https in it then wont it become httpss
@naresh would have been better if it was mentioned in the answer
1

In JavaScript grammar "-1" is usually used as sentinel value, and it comes from C.

String.prototype.indexOf method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

So you'll probably want to do something like :

setHttps(a) {
    var a = self.previewUrl.value;
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

1 Comment

a = a.replace('http', 'https')
1

Read about indexOf

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Update:

if(a.indexOf('http://') > -1){
   a = a.replace("http", "https");
}else if(a.indexOf('https') === -1){
   a = "https://" + a;
}

Comments

0

Check with this code

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Comments

0

Change

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

"http://www.example.com".indexOf('http://') outputs 0 as 'http://' is at that index in the provided URL.

Also, you need to assign the a.replace's output back to a, so the final code would look like this:

function setHttps(a) {
    if (a.indexOf('http://') > -1){
            a = a.replace('http', 'https');
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Comments

0

You need to replace this

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

It is because, indexOf() returns the index of the first occurrence of the string. Here in your case, the first occurrence of http:// is 0. But, your condition is true if indexOf() > 0. So you need to change the condition so that it would work for 0 also.

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.