我对JS或Jquery不太了解,所以我们非常感谢您的帮助。基本来说,我想使用随机网址而不是固定的网址。可以从外部平面文本文件http://example.com/url100.txt中获取随机url。
我在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"},而是使用一个随机的url。希望我的问题是清楚的
发布于 2015-02-28 06:47:15
试试这个:
// 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"
});
});https://stackoverflow.com/questions/28778796
复制相似问题