我认为没有“简单”的解决方案......而且 javascript 对于这样的目的似乎不是一个好主意。
你可以试试:
<base href="https://yourdomain.com/">
放置在文档标题中。
或者
你的想法:
<a href="/test-me1/">regular link 1</a>
<a href="/test-me2/">regular link 2</a>
<a href="/https/test-me3/">secure link</a>
并在底部,但在关闭body标签之前放置如下内容:
<script type="text/javascript">
(function(){
var h = 'yourdomain.com';
/* could be replaced with something like window.location.host */
var a = document.links;
for (var c = 0; c < a.length; c++) {
var i = a[c].href.indexOf('/https/');
if(-1 !== i)
{
a[c].href = 'https://' + h + '/' + a[c].href.substring( i + 7 );
/* where 7 is a length of '/https/' */
}
}
})();
</script>
或者甚至简单地:
<script type="text/javascript">
(function(){
var a = document.links;
for (var c = 0; c < a.length; c++) {
if(-1 !== a[c].href.indexOf('/https/') )
{
a[c].href = a[c].href.replace('/https/','').replace('http:','https:');
}
}
})();
</script>