Solved

setAttribute on Mac's Safari

Posted on 2006-11-27
3
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
Cem Türk 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table header must be on top 2 53
javascript delete path 7 42
isNaN issue on the Table input text 2 38
Drag & Drop Error 5 24
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

736 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