I agree with other posters that suggest that there are better ways to do what you need. With that said, it sounds like you're tied, so let me suggest a crack. (BTW, hat tip and +1 vote on protocol relative URL, I did not know about that!)
In any case, I assume that you are in the <a> tags, but this should be easily extrapolated to others:
if (document.location.protocol === 'https:') { $('a').each(function() { var href = $(this).attr('href'); if (href.indexOf('http:') > -1) { href = href.replace('http:', 'https:'); $(this).attr('href', href); } }); }
With this help, I would advise you to see if there is a safer / more practical way to do what you are trying to do. I also mentioned that this approach will most likely only work for links; changing CSS and script links after page loading will certainly have unpleasant consequences and will not give you the desired result.
Note the ":" in the document document.location.protocol === 'https:' ".
source share