Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6750043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:45:42+00:00 2026-05-26T12:45:42+00:00

I was reading Stoyan Stefanov’s excellent article about JS domain sharding for image files

  • 0

I was reading Stoyan Stefanov’s excellent article about JS domain sharding for image files and wanted to improve his recipe.

The below script will iterate through the images in a page, assign each to a bucket based on the length of the “src” value, and assign it to a bucket (logging this to the console).

What I’m trying to do is create a JQuery plugin that can accept a collection like $(‘img’) or $(‘link’) and rewrite the “src” attribute with a sharded domain like “http://images1.mydomain.com/path/to/image.png”.

If my page has:

<img src="/Images/file1.png" />
<img src="/Images/filenumber2.png" />
<img src="/Images/footer.png" />

I want to do something like:

$(document).ready(function(){
  $('img').domainShard(3, "subdomain", "mydomain.com")
});

To roughly produce:

<img src="http://subdomain1.mydomain.com/Images/file1.png" />
<img src="http://subdomain2.mydomain.com/Images/filenumber2.png" />
<img src="http://subdomain3.mydomain.com/Images/footer.png" />

Using a JQuery plugin like so:

(function ($) {
    $.fn.domainShard = function (buckets, subdomain, domain) {
        var numBuckets =  buckets || 3;
        var subdomain = subdomain || "images";
        var domain = domain || "mydomain.com";
        return this.each(function () {
            // look at the src
                // var src = $(this).attr('src');
            // compute bucket assignment
                // do stuff
            // set the new path
                // newSrc = $(this).attr('src', path);
        });
    };
})(jQuery);

Here is Stoyan’s Javascript:

function getBucket(url, numbuckets) {
  var number = url.length,
  group = number % numbuckets;
  return group;
}

function toBuckets(stuff, numbuckets) {
  var numbuckets = parseInt(numbuckets, 10),
  url, group,
  buckets = Array(numbuckets),
  cache = {};
    for (var i = 0, max = stuff.length; i < max; i++) {
        url = stuff[i].src;

        if (typeof cache[url] === 'number') {
            continue;
        }
        group = getBucket(url, numbuckets);
        if (!buckets[group]) {
            buckets[group] = [];
        }
        buckets[group].push(url);
        cache[url] = group;
    }
    return buckets;
}

console.log(toBuckets(document.images, 3));

I’ll keep hacking away and report back when I have something working — any advice or assistance is greatly appreciated.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:45:42+00:00Added an answer on May 26, 2026 at 12:45 pm
    $(document).ready(function () {
                $("img").domainShared("striano.net", "images", true);
            });
    
            $.fn.domainShared = function (topDomain, subDomain, useSSL) {
                return this.each(function () {
                    var _this = $(this)
                    if (useSSL) {
                        _this.attr("src", "https://" + subDomain + "." + topDomain + _this.attr("src"));
                    } else {
                        _this.attr("src", "http://" + subDomain + "." + topDomain + _this.attr("src"));
                    }
                });
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I have been reading Object-Oriented Javascript by Stoyan Stefanov, and at one point he
Reading this excellent answer about password hashing and wondering how to implement it: The
Reading manual about Sling http://sling.apache.org/site/46-line-blog.html added folder blog and blog.html to destination: \launchpad\content\src\main\resources\content\ but
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading some questions here on SO about conversion operators and constructors got me thinking
Reading the following article, I managed to put a legend outside plot. How to
Reading the Google Docs API I find this: Downloading Files cannot be downloaded in
Reading an article on the <article> tag on HTML5, I really think my biggest
Reading about the lack of persistence ignorance in Entity Framework I often stumble upon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.