Solved

setAttribute on Mac's Safari

Posted on 2006-11-27
3
633 Views
Last Modified: 2008-02-01
I am trying to set an image attribute using javascript. It works just fine in IE and firefox but not in Macintosh's Safari browser. Can anyone tell me if there is a way to do this? Here is my code:

function testfunc(id){
    newImg = new Image();
    newImg.setAttribute('id', id);
}

Thanks.
0
Comment
Question by:rbichon
3 Comments
 
LVL 17

Accepted Solution

by:
cem_turk earned 250 total points
ID: 18022217
try
newImg.id="id";
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 18022540
And what is the point of setting the ID in the first place?
0
 
LVL 1

Author Comment

by:rbichon
ID: 18022740
It really isn't just the id but a couple of other variables as well. Eventually I am going to include an onload event for the image so that a function will run once it is loaded. If the function runs before the image is loaded, it will not be able to properly determine the height and width of the image. To use the onload event on an image, the function it calls cannot have any parameters associated with it. So I am trying to pass parameters to the function by setting attributes to the image. Of course there are several scripts out there that will swap images but none of them will resize those images to a maximum width and height. Long story short (too late), cem_turk is correct. His code works in IE, FireFox, and Safari. Here is my code that will swap an image and resize the new image to a maximum of 250px in height and width:

function swapImage(id,path){
      var element = document.getElementById(id);
      element.src = "/images/spacer.gif";
      newImg = new Image();
      newImg.onload = resizeImage;
      pathatt = document.createAttribute('path');
      pathatt.nodeValue = path;
      newImg.id = id;
      newImg.src = path;
}

function resizeImage(){
      var element = document.getElementById(newImg.id);
      element.src = pathatt.nodeValue;
      if (newImg.width > newImg.height){
            ratio = newImg.height / newImg.width;
            if (newImg.width > 250){
                  w = 250;
            }else{
                  w = newImg.width;
            }
            element.style.width = w;
            element.style.height = Math.round(w * ratio);
            element.setAttribute('width', w);
            element.setAttribute('height', Math.round(w * ratio));
      }else{
            ratio = newImg.width / newImg.height;
            if (newImg.height > 250){
                  h = 250;
            }else{
                  h = newImg.height;
            }
            element.style.width = Math.round(h * ratio);
            element.style.height = h;
            element.setAttribute('width', Math.round(h * ratio));
            element.setAttribute('height', h);
      }
}

I had to use document.createAttribute because I can't seem to use setAttribute on an image in Safari if the attribute isn't already defined (i.e. width). If anyone knows how to do that in Safari let me know.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now