i'm using javascript code to redirect visitor to random link like the example below

but sometimes i get error 404 response :(

var pageArr = ["http://google.com", "http://yahoo.com", "http://bing.com", "http://youtube.com", "http://facebook.com"];
document.location.href = pageArr[Math.ceil(Math.random()*5)];

ps:the code does exist in external js file

what the problem here ?

i'm not coder ..thanks in advance

Recommended Answers

All 2 Replies

Try math.floor instead. You will then get numbers within the range of 0-4 which is what you want because you are using an array. pageArr[0] - pageArr[4]

thank u so much u helped me alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.