Skip to content

Instantly share code, notes, and snippets.

@0asa
Forked from aloncarmel/randombg.js
Last active April 18, 2023 15:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 0asa/3b7ffdd04e35b161e038cc2a71be4b7f to your computer and use it in GitHub Desktop.
Save 0asa/3b7ffdd04e35b161e038cc2a71be4b7f to your computer and use it in GitHub Desktop.
A minimal HTML example to grab a random background from unsplash.com using their API and apply to a div.
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
<script>
function GetRandomBackground()
{
var app_id = 'YOUR_APPLICATION_ID'
var url = 'https://api.unsplash.com/photos/random?client_id=' + app_id;
$.ajax({
url: url,
dataType: 'json',
success: function(json) {
var src = json.urls.regular;
$('#selector').css('background-image','url('+src+')').css('background-size','cover');
}
});
}
GetRandomBackground();
</script>
</head>
<div id="selector" style="width:100%; height:100%"></div>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment