Suppose that you need to enable SSL on your site and you have to change all your internal links from http
to https
. Normally you can do this manually or you can process your pages with a server-side script. However, if you have many pages and you are not accustomed with working with a server-side script, you can use jQuery for this task.
What you have to do is to simply loop through all the <a>
elements which point to your site (i.e., they're internal links) and replace all the instances of the http
substring in the href
attribute with https
:
var toSSL = function() {
var host = location.host;
$('a[*=' + host + ']', 'body').each(function() {
var $a = $(this);
var href = $a.attr('href');
if(href.indexOf('https') == -1) {
var ssl = href.replace('http', 'https');
$a.attr('href', ssl);
}
});
};
The above function also checks whether an internal link already points to SSL. If this is not the case, it replaces the substring http
with https
and resets the href
attribute of the current link.