Reputation: 357
I want to prepand http://
to every URL that doesn't begin with it, I used this:
if (val.search('http://') === -1) {
val = 'http://' + val;
}
The problem is that it appends http://
to URLs that begin with https//
I want to ignore both http://
and https://
.
Upvotes: 1
Views: 4061
Reputation: 150253
if (val.indexOf('http://') === -1 && val.indexOf('https://') === -1) {
val = 'http://' + val;
}
The regex
way is:
if (!val.search(/^http[s]?:\/\//)){
val = 'http://' + val;
}
Upvotes: 8
Reputation: 318508
if (val.indexOf('http://') === -1 && val.indexOf('https://') === -1) {
val = 'http://' + val;
}
You could also use a regex:
if(!/^https?:\/\//.test(val)) {
val = 'http://' + val;
}
Upvotes: 4