how to generate and append a random string using jquery

前端 未结 3 1262
鱼传尺愫
鱼传尺愫 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条回答
  •  梦毁少年i
    2021-02-08 05:17

    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.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)

提交回复
热议问题