将我网站中的所有链接更改为https

时间:2016-08-14 23:12:50

标签: javascript http https

我的博客网站https://www.arwikig.com/ 如你所见,我使用ssl,每个想法都很好,但在帖子中我看到错误“同样的事情不保存在这个网站”我看到我的网站太多链接在我的网站使用http ..即使在我的HTML所有链接都是'//',表示https ..&我试着用这个js。

        <script type='text/javascript'>
    //<![CDATA[
    function RedirNonHttps() {
        if (location.href.indexOf("https://") == -1) {
            location.href = location.href.replace("http://", "https://");
        }
    }
    //]]>
    </script>

&安培;太多像这样的其他脚本&amp;我没有看到任何影响 谢谢你的帮助和我我很抱歉我的英语不好

1 个答案:

答案 0 :(得分:1)

嗯,我猜你执行的功能正确吗?如果你不这样做,你可以自动执行此操作:

<!--This awesome script -->
<script type='text/javascript'>
    //<![CDATA[
        (function RedirNonHttps() {
            if (location.href.indexOf("https://") == -1) 
            {
                location.href = location.href.replace("http://", "https://");
            }
         })();
    //]]>
</script>

这种语法称为“自我调用函数”,有a link

更新#1 好吧,我正在您的网站上测试此代码,以更改每个'http'hrefsrc属性中的content

// 1. let's find those bad guys who are annoying us
// [href*='http:']    => Will find those with attribute href containing the string http
// [src*='http:']     => Will find those with attribute src containing the string http
// [content*='http:'] => Will find those with attribute content containing the string http

$("[href*='http:'], [src*='http:'], [content*='http:']").each(function(){

   // Let's save the reference of matched item
          var element = $(this);

   // 2. Get the attributes for the current matched element, it could has href, 
   // src or even content attributes, and finally replace them
          var href    = (element.attr("href")    || "").replace("http://", "https://");
          var src     = (element.attr("src")     || "").replace("http://", "https://");
          var content = (element.attr("content") || "").replace("http://", "https://");

   // 3. Now just update the new attributes with the fresh and sweet data
          element.attr("href"   , href);
          element.attr("src"    , src);
          element.attr("content", content);
});

希望它对你有所帮助。但我仍然认为你应该在你的代码中手动编写它。

哦,顺便说一下!要调用它,你应该将它包装在$(document).ready()$(function(){})中,因为jQuery需要整个页面满载才能工作:

// Common way... 
$(document).ready(function(){
    // - - The code that I wrote before
});

// Or you can use this way! (Pretty short and funny)
$(function(){
    // - - The code that I wrote before
});