mardi 11 septembre 2018

random image from google set as background using query for image keyword

i have this code:

<html>
<head>
    <title></title>
    <script src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load('search', '1');
    google.setOnLoadCallback(OnLoad);
    var search;

    //i suggest instead of this to make keywords list so first to pick random keyword than to do search and pick random image
    var keyword = 'cute kittens';

    function OnLoad()
    {
        search = new google.search.ImageSearch();

        search.setSearchCompleteCallback(this, searchComplete, null);

        search.execute(keyword);
    }

    function searchComplete()
    {
        if (search.results && search.results.length > 0)
        {
            var rnd = Math.floor(Math.random() * search.results.length);

            //you will probably use jQuery and something like: $('body').css('background-image', "url('" + search.results[rnd]['url'] + "')");
            document.body.style.backgroundImage = "url('" + search.results[rnd]['url'] + "')";
        }
    }
    </script>
</head>
<body>

it is supposed to give me a picture of a kitten then make that the background, however it is not giving me any background, does anybody have an idea on how to fix this? if you do, please help. this may seem like a duplicate, however i got the code from a previous post and it does not work, so please do not mark as duplicate, i have tried modifying the code in multiple ways, none worked, i lost the different edits when my device shut down so i dont have those.




Aucun commentaire:

Enregistrer un commentaire