Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Loading images using Javascript [modified]

Loading images using Javascript [modified]

Scheduled Pinned Locked Moved Web Development
helpjavascripthtmldatabasecom
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jszpila
    wrote on last edited by
    #1

    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: ================== ![](javascript:GetName('name')) 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?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups