使用随机URL而不是固定URL

时间:2015-02-28 06:13:57

标签: javascript jquery

我对JS或Jquery不太了解,所以任何帮助都表示赞赏。基本上我想使用随机网址而不是固定网址。可以从外部平面文本文件中获取随机URL http://example.com/url100.txt

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

<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”},我想使用随机网址。希望我对我的问题很清楚

1 个答案:

答案 0 :(得分:0)

试试这个:

// 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:

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

});