javascript - How to get the image by ID from image Slider and store it to the cookies? -
i have image slider of 5 pics in home page in asp.net mvc application. when user click on other menu tab within application last seen image of image slider should stored in cookies. when user come homepage,image slider start last seen image . chtml:
<div id="sliderframe"> <div id="slider"> <a id="image1" href='#' class='linkdisabled'> <img src="~/content/internal/images/image1.jpg" alt="" /> </a> <a id="image2" href='#' class='linkdisabled'> <img src="~/content/internal/images/image2.jpg" alt="" /> </a> .....
i thinking of overwriting images id in cookies , when user homepage last stored image in cookies shown in image slide. how image id , store cookies? please help.
first, container of links , collection of links inside container
var slider = document.getelementbyid("slider"); var links = slider.getelementsbytagname("a");
next, initialize method set cookie
function setcookie(){ document.cookie="lastseenimageid=" + this.id; }
add event listeners each link. note: in example, not consider support of internet explorer < 9, because not know whether use jquery or own methods cross-browser compatibility:
for(var i=0; i<links.length; i++){ links[i].addeventlistener("click", setcookie, false); /*(function(j){ links[j].attachevent("onclick", function(){ setcookie.call(links[j]); }); })(i);*/ }
last, fire click event when page loaded
function fireanevent(){ var cookie = document.cookie; if(cookie && document.cookie.test(/lastseenimageid=([^;]+)/)){ var link = document.getelementbyid(regexp.$1); link.click(); } } window.addeventlistener("load", fireanevent, false); // window.attachevent("load", fireanevent);
this code example, , may require correction, according needs.
Comments
Post a Comment