0

I stuck when experimenting with blogger thumbnails. I don't want to use blogger default thumbnails since the size just 72px, it's very small. So, I found a method by rewriting the URL of the image source (the image hosted by google blogger service). For example, I have an image in this URL http://4.bp.blogspot.com/-XfeUAQMRZnk/VBw8Gv0tZvI/AAAAAAAAAXM/DnmxYqUROVc/s1600/home%2Bthumbs.jpg, the image will loaded with max-width 1600px, indicated by the /s1600/ in the URL. I want to load the image at 300px width, then the URL will be http://4.bp.blogspot.com/-XfeUAQMRZnk/VBw8Gv0tZvI/AAAAAAAAAXM/DnmxYqUROVc/s300/home%2Bthumbs.jpg. Also, the image served in HTTP protocol by default, but it's possible to served in HTTPS just by adding https:// as the protocol.

This is my thumbnail markup:

<div class="thumbnail">
    <a class="thumbTooltip" href="#" title="Flat Design Sample">
        <img alt="Flat Design Sample" src="http://4.bp.blogspot.com/-XfeUAQMRZnk/VBw8Gv0tZvI/AAAAAAAAAXM/DnmxYqUROVc/s1600/home%2Bthumbs.jpg">
    </a>
</div>

The question is, how I can rewrite the default image URL by using javascript method? I want to force the image served in HTTPS by rewriting http:// to https://, and served in 300px width by rewriting s1600 to s300. The final URL will look like this: https://4.bp.blogspot.com/-XfeUAQMRZnk/VBw8Gv0tZvI/AAAAAAAAAXM/DnmxYqUROVc/s300/home%2Bthumbs.jpg

0

2 Answers 2

1

The code is inside an window.onload to ensure that all elements are there. Then in the url-string the first replace() changes the protocol. The second uses a regular expression to find the segment where the size is defined and changes it to s300. It will work even when the images comes from another directory.

window.onload = function() {
    var img = document.querySelector('.thumbnail img');
    img.src = img.src.replace('http', 'https').replace(/\/s\d+(?=\/)/, '/s300');
};

Instead of the window.onload wrap you also can put the code in <script>-tags just before </body>.

Sign up to request clarification or add additional context in comments.

4 Comments

it works for me. in my document, the url are re-written after the original image fully loaded. is it possible to load the image only after the source re-written? so the browser not load the image twice.
@AnderMorales As long as the url is hardcoded in html it's not possible. To get and change the src the script must wait until the image tags are parsed, but in the moment they are parsed the browser starts loading.
@AnderMorales It prevents the original image from loading if you place the code (without window.onload) in a <script> direct after the <img>. But if you can do that you can also change the url directly.
It's ok. The main point is you just solve my problem. thanks!
0

Simple replacement of a token.

function getThumbnailUrl (element)
{
    $url = element.href.split("/");
    $url[7] = "s300"; 
    return $url.join("/");
}

4 Comments

OP want to change the protocol too.
simply set $url[0] = "https:"; but that should have been self explaining
btw why there is no selector inside the script? such as .thumbnail. I'm sorry, I'm not very fluent in javascript.
selector, .thumbnail - you are refeering to jQuery there. I told you how to rewrite a GIVEN element. you can combine it with jQuery as jQuery is simply a javascript framework. e.g. call it like $(".thumbnail").each(function(obj){ $(obj).attr({"href":getThumbnailUrl(obj)}); }); - you really should look up the documentation of jQuery to understand how it works. I'm not going to write your entire website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.