I'm having an issue getting IE 7 to respond to the setAttribute("src", "...") command in javascript.

The code does the following:
1) There exists an img id called mainImage in my html.
2) On load the body calls getimagesource
3) The javascript reads a configuration file (test3.txt), that has each of the following, newline delimited, inside it: filename (1.jpg), date (2009-01-19), pic title(Digital), pic caption (This is a pic of...).
4) The code then attempts to set the src of the image to the image that has the closest date before the current day.
5) The it sets the caption, and once the image is loaded, resizes it.

The code works as expected in firefox. You can see it here:

The code produces no errors in IE7, just fails to run. Due to the fact that in IE 7 the title and caption of the image appear correctly, I concluded that the xmlhttprequest worked. That would indicate the problem is with the setAttribute tag. As you can see, I also tried using the HTML DOM 2 method, (declaration before [line 58], set after [line 60]) around the setAttribute tag. I would greatly appreciate any assistance.

My code is attached below.

function sizeIMG(imgid){

	var new_image_size = 600;

	var el = document.getElementById(imgid)
	if(el.width > 600 || el.height >600) {
		var tempwidth = el.width;
		var tempheight= el.height;

function getimagesource() {
var txtFile = new XMLHttpRequest();
//txtFile.open("GET", "http://shenet.org/high/hsacaddept/technology/maintechweb/imageconfig.txt", true);
txtFile.open("GET", "http://shentech.org/techdept/config/test3.txt", true);
txtFile.onreadystatechange = function() {

  if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.

  if (txtFile.status === 200) {  // Makes sure it's found the file.
     var allText = txtFile.responseText;
	 var lines = txtFile.responseText.split("\n"); // Will separate each line into an array
	 var numlines=lines.length;

var i=1;
var d = new Date();
var datenow = new Date();
	  while(numlines>i && datenow<d){
	   var curdate = lines[i].split("-");
	  var message = new Array('./images/'+lines[i-1], lines[i+1], lines[i+2]);	 
	  var img = document.getElementById("mainImage");
	  document.getElementById("mainImage").setAttribute("src", message[0]);
		img.src = message[0];

	var imageObj = new Image();
		imageObj.onload = function() {
	imageObj.src = message[0];
		document.getElementById("title").innerHTML = message[1];
		document.getElementById("caption").innerHTML = message[2];  

Thanks in advance,

I managed to get it to work in IE7 with help from Aankhen in #javascript on irc @ freenode. However, a solution could not be found with setAttribute or the DOM 2 img.src.

To go against the grain, I'm actually going to post the solution, so the next poor soul doesn't have to deal with IE7 being horrible. Replace setAttrib with:

var oldImage = document.img; //img being the id or name of your image

var newImage = document.createElement("img"); 
newImage.src = message[0];  //set all your properties in here
newImage.id = 'mainImage';
oldImage.parentNode.replaceChild(newImage, oldImage); 
//its magic ... Dom sucks
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.