Solution 1 :

To achieve this you can use Math.random(). That function generates a random number between 0 and 1. As you want a 50/50 split you can put a condition on that value so that if it’s under 0.5 the redirect happens. Try this:

error: function() {
  if (Math.random()< 0.5)

Problem :

So I just created a test Ajax call function in my script to apply a minimal protection to my website which looks like this:

function testPHP() {
    type: 'post',
    url: 'file.php',
    success: function() {
    error: function() {
      window.location.href = "";

And I execute it right after $(document).ready(function(). So basically if the file “file.php” doesn’t exist, it will redirect the user to my website. So what I need is to redirect only a % of the users by the window.location.href.

Let’s say 5/10 of the users entering the website will be redirected to mywebsiteurl and the other 5 will stay on the page. Is there a way to do this?


Comment posted by hhamm

you could use a random function to do the job: e.g. Math.random() < 0.5 to specify 50% redirects