samedi 20 juillet 2019

How to open a specific url page everytime it is reloaded from the browser using javascript?

I have a link that generates new images if you change the number at the end of the URL. I want that each time I refresh the page from the browser, it should generate a different image. I don't want to use any button to click on but to use the refresh button in the browser. I tried using the window.onload property but am facing some issue.

I have the code almost working and I am able to generate a new url with the random number in the end every time I refresh the page but when I try to load it's page it doesn't work. It gets stuck to the first random url. I know that the url is being changed randomly because from the alert message I can verify that. kindly help!

<script>
var url = "https://memegenerator.net/img/images/501.jpg";
function load()
{
    var num=url.match(/\d/g).join("");
    var numreplace=Math.floor(Math.random()*1000);
    url=url.replace(num,numreplace);

    openpage(url);
}

function openpage(url) {
    alert(url);
    //window.location.href=url;
    //or window.location.replace(url);
}

window.onload=load();
</script>

I expect a new image to be loaded every time I reload the page. Thanks!




Aucun commentaire:

Enregistrer un commentaire