Loading images using Javascript [modified]
-
Hey All, I'm having a bit of a problem with loading some images using javascript. What I'm trying to do is parse out a query string value, and load an image using that value. For instance, if someone went to: http://www.mydomain.com?person=JohnDoe It would parse out the name from the query string and load JohnDoe.gif into a image named portrait and write a cookie with that information; if there were no query string value, it would would load a default image. Now, this works in Firefox, but not IE; IE just results in that irritating little red "X" broken image box. My Javascript: ======================== //get name from GET string function GetName(CookieKey) { //try to read the key "name" value from cookie var personalimg = readCookie('name') if (personalimg) //if the cookie exists, load the image with the same name { //this appears to be the part that isn't working in IE only (firefox is fine) //I have checked the image paths and the files do exist document.contact.src="./images/people/" + personalimg + "Contact.gif"; } else // if no cookie, check for name in query string or load default image { var query = window.location.search.substring(1); var pair = query.split("="); //if the cookie contains the key name, load it's value into the image if (pair[0] == CookieKey) { document.contact.src="./images/people/" + pair[1] + "Contact.gif"; document.cookie = "name=" + pair[1] + ";" } else //load default image { document.contact.src = "./images/Contact.gif"; } } } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } ================== My HTML: ================== ) Note that I don't use Javascript too often so any suggestions on how to improve this would be much appreciated, as is any other help. Thanks in advance! ------------------- abort, retry, fail?