Reputation: 145
// these files are stored on my system on these path : C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/
var gifArr = ['source.gif',
'source1.gif',
'source2.gif',
'source3.gif',
'source4.gif',
'source5.gif',
'source6.gif',
'source7.gif',
'source9.gif',
'source10.gif',
'source11.gif',
'source12.gif',
'source13.gif'
];
// function to select the random gif andchange the gif after 5 seconds
function changeGif(){
setInterval(function(){
$('.add-gif').css({'background-image': 'url(C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/' + gifArr[Math.floor(Math.random() * gifArr.length)] + ')'});
},
5000);
}
the function changeGif() will select the random gif and change the background after 5 seconds. the function will work on my system as I defined the static path in the function. But I want that the user who downloads my application and run code, then the path will be set according to the user's system. So how can I get a path of the files according to the user's system?
Upvotes: 0
Views: 30
Reputation: 1330
You need to supply the relative path of the image from the script file
According to W3
Partial URLs are interpreted relative to the source of the style sheet, not relative to the document
The url is relative to the location of the file, so this should work for you:
url('../../images/image.jpg')
$('.add-gif').css('backgroundImage','url(images/' + gifArr[Math.floor(Math.random() * gifArr.length)] ) );
Upvotes: 1