如何使用jQuery生成和附加随机字符串 [英] how to generate and append a random string using jquery

查看:186
本文介绍了如何使用jQuery生成和附加随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery或javascript将随机字符串附加到元素的属性上.

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

我需要引用CDN上的CSS文件.不幸的是,每次更新CDN时,CDN都会更改此CSS文件的URL.因此,我不能简单地引用静态URL.

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.

事实证明,如果将字符串附加到CDN之前从未见过的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.

例如

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

我知道,这是丑陋的,有缺陷的和疯狂的.但是有时候这就是cookie崩溃的方式.另一种选择是尝试以同等的方式维护不断增长的CSS文件和模板头文件……这是不可行的. ;)

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

我的jQuery技能很少.我发现有两个不同的代码可以自己完成我需要的工作,但是我一辈子都无法弄清楚如何使它们协同工作.

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.

        // 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) )
            );
    }

代码#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 ); 

我假设需要代码#2" :我的假设是,只要将随机数附加到现有"href"属性的末尾,就不会发生任何事情.也就是说,CSS文件不会会重新加载.

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! :)

乔恩

推荐答案

保持简单,将当前时间戳(以毫秒为单位)应用于URL:

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

请注意,这确实是个坏主意,如果您不打算对样式表进行太多更改,只需在文件中手动替换URL即可.有用于此目的的工具(在基于Linux的系统中为sed)

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)

这篇关于如何使用jQuery生成和附加随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆