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 6231629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:58:08+00:00 2026-05-24T09:58:08+00:00

Generalities I’d like to append a random string to an element’s attribute, using either

  • 0

Generalities

I’d like to append a random string to an element’s attribute, using either jQuery or javascript.

Specifics

I need to reference a CSS file that lives on a CDN. Unfortunately, the CDN changes the URL of this CSS file every time the file is updated. So I can’t simply refer to a static URL.

It turns out tho, that if you append a string to the end of the URL that the CDN has never seen before, it will return the most recent version of the file. Bogus to be sure.

e.g.

<link href="http://example.com/style.css?randomString-neverSeenBefore">

I know, this is ugly, faulty and insane. But sometimes that’s how the cookie crumbles. The alternative would be to attempt to maintain, in parity, a growing battery of CSS files and template headers… not viable. 😉

What I’ve got so far,

My jQuery skills are meager. I’ve found two different bits of code that do what I need on their own but I can’t for the life of my figure out how to get them to work together.

bit of code #1:

        // this guy is a random number generator that i found
        jQuery.extend({
        random: function(X) {
            return Math.floor(X * (Math.random() % 1));
        },
        randomBetween: function(MinV, MaxV) {
          return MinV + jQuery.random(MaxV - MinV + 1);
        }
    });


    // using above plugin, creates 20 random numbers between 10 and 99
    // and then appends that 40 digit number to a paragraph element
    for (i = 0; i < 20; i++) {
        $('p').text(    
            $('p').text() + ($.randomBetween(10, 99) )
            );
    }

bit of code #2:

    // this fellow creates a link to the style sheet
    // after the page has loaded
    var link = $("<link>");
    link.attr({
            type: 'text/css',
            rel: 'stylesheet',
            href: 'http://example.com/style.css'
    });
    $("head").append( link ); 

I assume “bit of code #2” is required: my presumption is that were I to simply appended a random number to the end of an existing “href” attribute, nothing would happen. i.e. the CSS file would not be reloaded.

Thanks a million for your help on this! 🙂

Jon

  • 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-24T09:58:09+00:00Added an answer on May 24, 2026 at 9:58 am

    Keep it simple, apply the current timestamp in miliseconds to the URL:

    // this fellow creates a link to the style sheet
    // after the page has loaded
    var link = $("<link>");
    link.attr({
            type: 'text/css',
            rel: 'stylesheet',
            href: 'http://example.com/style.css?' + (new Date).getTime()
    });
    $("head").append( link );
    

    Please note that this is really a bad idea, if you’re not going to change your stylesheet much, just replace the URL manually in the files. There are tools for that purpose available (sed under Linux-based systems)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm asking in generalities - why would any server not set and return headers
I am familiar with the generalities of genetic programming but am wondering where i
I need to bone up on my CLR compiling knowledge, so i'm going to
I'm building a poll widget using ASP.NET controls and Linq-to-Sql for a high traffic
Under what circumstances would something like eventlet/gevent be better than twisted? Twisted seems like
I am developing and doing all the testing on a local machine using PHP
[ Preface: The associative C++ containers like std::map are a bit like micro-databases with
On a bit of a learning curve. Know one of you gurus can help
I am trying to figure out how to best convert many of may apps
could anyone please clarify the meaning of line generalizes the tag object's storage of

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.