Islam Scarlet
Islam Scarlet

Reputation: 31

change all links in my website to https

My site in blogger with the domain https://www.arwikig.com/ As you see I use ssl, and every think is good but in the posts I see the error "same things not save in this site" and I see in my site too many links in my site use http .. Even though in my html all links are '//' it mean https .. & I try to use this js.

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

& too much other scriptes like this & I dont see any effects thanks for help me & I'm sorry for my bad english

Upvotes: 2

Views: 4486

Answers (1)

Jeancarlo Fontalvo
Jeancarlo Fontalvo

Reputation: 128

Well I guess you executed the function right? if you didn't you can make it automatically doing this:

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

This syntax is known as "Self invoking function", there's a link

UPDATE #1 Well, I was testing this code in your website, to change the 'http' in each href, src or content attributes.

// 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);
});

Hope it helped you. But I still considering that you should write manually this in your code.

Oh, by the way! To call it, you should wrap it in a $(document).ready() or $(function(){}), because jQuery needs that the whole page be full loaded to works:

// 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
});

Upvotes: 2

Related Questions