Solved

setAttribute on Mac's Safari

Posted on 2006-11-27
3
637 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

772 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