473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

image replacement failing in safari

Appreciate any advice:

I'm working on a small script to swap out an image using javascript, this is my first attempt at using javascript. My method works fine in Firefox, but not in Safari. Documentation for Safari is a bit thin, so I thoughtt maybe someone here might recognize the issue quickly.

I'm using two methods to apply the image swap, one addresses the element ID, and the other the TagName.

My .JS:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function randomImage(x) {
  3.  
  4.     if (saf)
  5.     {
  6.         var item = document.getElementById(x);
  7.         item.style.background = 'url(../graphics/header_image1.jpg)';
  8.     }
  9.  
  10.     else {
  11.  
  12.         var ITEM = document.getElementById(x);
  13.         var random_i = String(Math.round(Math.random()*8));
  14.         var rand_path = 'url(../graphics/header_image' + random_i + '.jpg);';
  15.         setRandom(x, rand_path)
  16.     }
  17. }
  18.  
  19.  
  20. function randomIm(x) {
  21.     var ITEM = document.getElementsByTagName(x).item(0);
  22.     var random_i = String(Math.round(Math.random()*8));
  23.     var rand_path = 'url(..\/graphics\/header_image' + random_i + '.jpg);';
  24.     setRand(x, rand_path)
  25. }
  26.  
  27.  
  28.  
  29. function setRandom(id,imag) {
  30.     var item = document.getElementById(id);
  31.     item.style.background = imag;
  32. }
  33.  
  34. function setRand(tag,imag) {
  35.     var item = document.getElementsByTagName(tag).item(0);
  36.     item.style.background = imag;
  37. }
Then in the html:

[HTML]<body onload="randomImage('headbar'); randomIm('h1'); randomIm('h2');">

<div class="top" id="headbar"></div>
<div class="rule"></div>
<div class="mid" ><h1></h1></div>
<div class="rule"></div>
<div ><h2></h2></div>
<div class="rule"></div>
<div class="footer" ></div>
</body>
[/HTML]

I have css styles set up to apply to each of the classes, IDs and 'h' tags. The idea being that I can decide where to apply the random script by targeting individual elements.

Also, i look forward to hearing if there are preferred methods that I'm not using. :o

Thanks much,
-greg
Aug 30 '06 #1
1 1852
acoder
16,027 Expert Mod 8TB
The semi-colon after the url(..) affects Safari. Remove that and it should work across all browsers.

Also, the Math.round(Math.random()*8) statement would give random numbers between 0 and 8, but not completely randomly. Use Math.floor instead:
Expand|Select|Wrap|Line Numbers
  1. var rand_path = 'url(../graphics/header_image' + Math.floor(Math.random()*9) + '.jpg)';
May 3 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Kerberos | last post by:
I followed Dan Cederholm's image replacement tutorial, to replace a header tag by a logo. The h1 is clickable if no CSS is applied but it I replace it by the logo, the area isn't clickable anymore...
2
by: Lutz Ißler | last post by:
Hi all! I dymically replace the child nodes of a DIV element by image nodes. In IE and Firefox this works properly. In Safari it works properly, too - but only if the site is called locally, eg....
2
by: Martin Honnen | last post by:
I was playing around with canvas support in recent Safari, Mozilla and Opera (only version 9 preview) but run into issues with Safari related to the very old DOM Level 0 Image object for preloading...
15
by: Alan Silver | last post by:
Hello, I am trying to do a simple (ha ha) bit of image replacement, so that the 'v' in a string of text inside an <h3> is replaced with an image of a tick that looks like a 'v' to fit with a...
4
by: yavannadil | last post by:
Hello! I use the following to translate button names in Google Maps: G_NORMAL_MAP.getName = function(short) { if(short) {return "Karta"} return "Karta"; } It works (tested) with Firefox...
7
by: fredo | last post by:
I've studied Eric Meyer's pure css popups, version two: http://meyerweb.com/eric/css/edge/popups/demo2.html which pops up an image when I roll over a text link. Now I want to pop up a large...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
7
by: David Stone | last post by:
Run into something recently that has left me a little puzzled. According to the examples in section 13.6.1 of html 4.01... <http://www.w3.org/TR/html401/struct/objects.html#h-13.6.1.1> I...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.