how to generate and append a random string using jquery

前端 未结 3 1318
鱼传尺愫
鱼传尺愫 2021-02-08 04:36

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 f

3条回答
  •  自闭症患者
    2021-02-08 05:20

    You can get just part of the date, for example:

    var d = new Date;
    
    // this string will change once a day for the same user
    var rnd_str_day = d.getFullYear() + '_' + d.getMonth() + '_' + d.getDay();
    href= 'http://example.com/style.css?' + rnd_str_day;
    
    // this string will change once an hour
    var rnd_str_hour = d.getFullYear() + '_' + d.getMonth() + '_' + d.getDay() + '_' + d.getHours();
    href= 'http://example.com/style.css?' + rnd_str_hour;
    

    And so the css will be uncached once a day or an hour, depending on your requeriments.

提交回复
热议问题