Reputation: 179
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() {
$.ajax({
type: 'post',
url: 'file.php',
success: function() {
alert("mafak");
},
error: function() {
window.location.href = "https://mywebsiteurl.com";
}
});
}
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?
Upvotes: 0
Views: 35
Reputation: 337637
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)
window.location.assign('https://mywebsiteurl.com');
}
Upvotes: 1