1

I dont know much about JS or Jquery so any help is appreciated. Bascially I want to use random url instead of fixed one . A random url can be fetched from an external flat text file http://example.com/url100.txt

The current code that I am using inside the section of the html page is as follow

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

So basically instead of twitter: {url:"http://example.net"}, i want to use a random url. Hope I am clear with my question

1 Answers1

0

Try this:

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

}); 

As you said you have use it on BLOGSPOT, and text file exists on another server. Then use this solution.

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

});
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65