Search code examples
javascriptjquerygoogle-chrome-extensionhref

Editing start of a[href] http:// - https:// jQuery


I am creating a Chrome extension which detects if the urls from Google search results are http:// and if so make it https://.

I have done this so far.

$('a[href^="http://"]').attr('href', 'https://' + "a[href]");

Any help will be appreciated. Thanks :)


Solution

  • You need to replace the occurrence of http:// with https://

    $('a[href^="http://"]').attr('href', function(i,oldhref){
       oldhref.replace("http://","https://")
    });
    

    or

    you can also use .each() to iterate over them:

    $('a[href^="http://"]').each(function(){
       $(this).attr('href', $(this).attr('href').replace("http://","https://"))
    });