2. [20pts] Assume your website uses a single cookie called images. The cookie st
ID: 3843632 • Letter: 2
Question
2. [20pts]
Assume your website uses a single cookie called images. The cookie string might look something like:
This cookie stores the user preferences for the images to be displayed on the website. Your website contains as many img elements as there are file names in the cookie string. Write a function that uses the cookie to set the src attribute for each img element to a file name from the cookie string. The first image element in the document should get the first file name in the cookie string, the second image element should get the second file name etc.
Explanation / Answer
//Function in jquery
//store the webiste cookie into a variable
var cookieString = document.cookie;
var filenames = cookieString.split('^');
var i=0;
// loop over all elements on the webiste and check if it's an img tag
$('*').each(function(){
if ($(this).is('img')) {
$(this).attr('src')=filenames[i];
i+=1;
}
});
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.