Solved

setAttribute on Mac's Safari

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now