首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用随机URL代替固定URL

使用随机URL代替固定URL
EN

Stack Overflow用户
提问于 2015-02-28 06:13:56
回答 1查看 87关注 0票数 1

我对JS或Jquery不太了解,所以我们非常感谢您的帮助。基本来说,我想使用随机网址而不是固定的网址。可以从外部平面文本文件http://example.com/url100.txt中获取随机url。

我在html页面的部分中使用的当前代码如下

代码语言:javascript
复制
<script type='text/javascript'>
  //<![CDATA[
  var mylinks = "http://google.com";
jQuery(document).ready(function ($) {
  $('#default-usage .to-lock').sociallocker({

    buttons: {order:["facebook-like","twitter-tweet","google-plus"]},

    twitter: {url:"http://google.com"},
    facebook: {url:"http://google.com"},
    google: {url:"http://google.com"},

    text: {
      header: "Like us To Unlock This Content",
      message: "This content is locked. Like us on Twitter, Facebook or Google plus to unlock it."
    },

    locker: {close: false, timer: 0,},
    theme: "secrets"
  });
});
  //]]>
</script>

基本上,我不想使用twitter:{ url :"http://example.net"},而是使用一个随机的url。希望我的问题是清楚的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-28 06:47:15

试试这个:

代码语言:javascript
复制
// Make sure the file is on the same server.
var file = "http://example.com/url100.txt";
$.get(file,function(txt) {

    // Split data and convert it into array
    var lines = txt.responseText.split("\n");
    var link = lines[Math.floor(Math.random() * lines.length)];

    // Apply plugin here
    $('#default-usage .to-lock').sociallocker({

        buttons: {order:["facebook-like","twitter-tweet","google-plus"]},
        twitter: {url:link},
        facebook: {url:link},
        google: {url:link},

        text: {
            header: "Like us To Unlock This Content",
            message: "This content is locked. Like us on Twitter, Facebook or Google plus to unlock it."
        },

        locker: {close: false, timer: 0,},
        theme: "secrets"
    });

}); 

正如您所说的,您已经在BLOGSPOT上使用了它,并且文本文件存在于另一台服务器上。那就用这个解决方案。

解决方案2:

代码语言:javascript
复制
jQuery(document).ready(function ($) {

    // Add link one by one in ""
    var social_links = ["link1", "link2", "link3", "link4", "link5", "link6"];

    var link = social_links[Math.floor(Math.random() * social_links.length)];
    console.log(link);
    // Apply plugin here
    $('#default-usage .to-lock').sociallocker({

        buttons: {order:["facebook-like","twitter-tweet","google-plus"]},
        twitter: {url:link},
        facebook: {url:link},
        google: {url:link},

        text: {
            header: "Like us To Unlock This Content",
            message: "This content is locked. Like us on Twitter, Facebook or Google plus to unlock it."
        },

        locker: {close: false, timer: 0,},
        theme: "secrets"
    });

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28778796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档