Change Image on Every Page Load using Random Number Generator
How to Change Image on Every Page Load using Random Number Generator
You can use the below code to change Image on Every Page Load
JS Code:-
var length;
$(document).ready(function() {
getFoldersTraining_dev();
});
function getFoldersTraining_dev() {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('/teams/FMHub/Quotes_image_homepage')?$expand=Folders,Files",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }, // return data format
success: function (data) {
var html = "";
if(data.d.Files.results.length >0){
length = data.d.Files.results.length;
var x = Math.floor((Math.random() * (length - 1)));
FileURL = data.d.Files.results[x].ServerRelativeUrl;
if(FileURL != ""){
html ="<img src='"+FileURL+"'></a>";
}
$("#quote_img_homepage").html(html);
}
else{
html = "Comming Soon";
}
$("#quote_img_homepage").html(html);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}
You can use the below code to change Image on Every Page Load
JS Code:-
var length;
$(document).ready(function() {
getFoldersTraining_dev();
});
function getFoldersTraining_dev() {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('/teams/FMHub/Quotes_image_homepage')?$expand=Folders,Files",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }, // return data format
success: function (data) {
var html = "";
if(data.d.Files.results.length >0){
length = data.d.Files.results.length;
var x = Math.floor((Math.random() * (length - 1)));
FileURL = data.d.Files.results[x].ServerRelativeUrl;
if(FileURL != ""){
html ="<img src='"+FileURL+"'></a>";
}
$("#quote_img_homepage").html(html);
}
else{
html = "Comming Soon";
}
$("#quote_img_homepage").html(html);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}
Comments
Post a Comment