|
Hello, I hope someone can help!!
I have some code to produce a random background image which is pulled in to the TD tag using an ID="bgimage1" statement - this works great on IE, however I need it to work on Netscape 4!
Is this because of the ID or the getElementById?
This is the JS statement:
document.getElementById(`bgimage1`).style.background = `url(` + image1[titleIndex] + `)`;
image1[titleIndex] is the random part
Please help!
Thanks
Andrew
|
|
|
Hi Andrew.
Here some handy code you can use to reference an object safely in any browser.
Rather than using document.getElementById(`id`), document.all[`id`], etc. You can use findDOM(`id`, 0) or findDOM(`id`, 1).
findDOM(`object`), 0) is like document.object
and findDOM(`object`, 1) is like document.object.style
The function just uses the supported DOM type for the browser and returns the object reference; getElementById, Layers or All. Browser details are first stored in global variables so you can use them elsewhere.
You should put the follow code in a .JS file so you can easily move it around and include it into pages.
Hope it helps you and anyone else out.
Cheers,
James
|
|
|
Here it is... (Just trying to deal with the dodgy Codetoad.com forum.).
Remove the remove the /`s from any "pa//rseInt", stupid forum.
var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;
if(document.getElementById)
{
isID = 1;
isDHTML = 1;
}
else
{
if(document.all)
{
isAll = 1;
isDHTML = 1;
}
else
{
browserVersion = pa//rseInt(navigator.appVersion);
if((navigator.appName.indexOf(`Netscape`) != -1) && (browserVersion == 4))
{
isLayers = 1;
isDHTML = 1;
}
}
}
function findDOM(objectID, withStyle)
{
if (withStyle == 1)
{
if (isID)
{
return (document.getElementById(objectID).style);
}
else
{
if (isAll)
{
return (document.all[objectID].style);
}
else
{
if (isLayers)
{
return (document.layers[objectID]);
}
}
}
}
else
{
if (isID)
{
return (document.getElementById(objectID));
}
else
{
if (isAll)
{
return (document.all[objectID]);
}
else
{
if (isLayers)
{
return (document.layers[objectID]);
}
}
}
}
}
|
|
|
Thanks James
Thats extremely helpful
Andrew
|
|
|
No problem Andrew, glad I could help.
|
|
|
|
|
|
|
|