0

I wanted to check if all links of a page have https:// or they just start with www. and if they don't have https:// just add it to them

I'm stuck in the condition to check if they have https: or not and how to add them https:

$('.rest-web a').each(function(){
    console.log($(this).attr('href'));
    var link = $(this).attr('href');
    if(link.location.protocol !== 'https:'){
      console.log('no');
      
    } else {
      console.log('yes');
    }
  });

Many thanks

Vander
  • 79
  • 9

1 Answers1

-1

Check this question: Prepending "http://" to a URL that doesn't already contain "http://"

Chose your solution for the check on the link.

In your example you can replace

console.log('no');

With something like:

$(this).attr('href', 'https://' + link)

Also keep in mind links going to non http(s) protocols, and anchor tags not having an href attribute at all.

Piemol
  • 857
  • 8
  • 17