does browser support an image using style ?

M

MV

How do I check if a browser supports an image using style before
writing it with document.write?

document.write("<img id='picture1' src='a.gif' alt='blah'
style='blah'>");
NN4 and Opera 6 throw a wobbler.

Also, what way is best to reference it?
document.getElementById('picture1')
or
document.images['picture1']
 
M

Michael Winter

How do I check if a browser supports an image using style before
writing it with document.write?

You can't. Browsers don't (at least generally) contain feature flags.
You'll just have to hope.

Don't forget, CSS and JavaScript are in the same catagory: support for
both is *optional*.
document.write("<img id='picture1' src='a.gif' alt='blah'
style='blah'>");
NN4 and Opera 6 throw a wobbler.

How do define, "a wobbler"? Do they ignore the style, or do they do
something worse like crash? If it's the former, you shouldn't worry. As
long as the content is still readable, that's all that is important[1].
You shouldn't strive for your pages to look exactly the same in all
browsers as it's not just possible, especially when the browsers in
question are outdated.
Also, what way is best to reference it?
document.getElementById('picture1')
or
document.images['picture1']

You can use both, but the latter is more widely supported.

var imgRef = null;

if( document.getElementById ) {
imgRef = document.getElementById( 'picture1' );
} else if( document.images ) {
imgRef = document.images[ 'picture1' ];
}

if( imgRef ) {
// Got a reference to the image
}

Mike


[1] You should, after all, make sure that pages are usable with CSS and
JavaScript disabled, when possible. They probably won't be pretty though,
but it's the content that's the most important.
 
M

MV

Michael Winter said:
How do define, "a wobbler"? Do they ignore the style, or do they do
something worse like crash? If it's the former, you shouldn't worry. As
long as the content is still readable, that's all that is important[1].
You shouldn't strive for your pages to look exactly the same in all
browsers as it's not just possible, especially when the browsers in
question are outdated.
Also, what way is best to reference it?
document.getElementById('picture1')
or
document.images['picture1']

You can use both, but the latter is more widely supported.

var imgRef = null;

if( document.getElementById ) {
imgRef = document.getElementById( 'picture1' );
} else if( document.images ) {
imgRef = document.images[ 'picture1' ];
}

if( imgRef ) {
// Got a reference to the image
}

Mike


[1] You should, after all, make sure that pages are usable with CSS and
JavaScript disabled, when possible. They probably won't be pretty though,
but it's the content that's the most important.

Sorry Michael, I had a tantrum instead of checking things out
properly. It was nothing to do with style, here's what I had:

document.write("<img id='picture1' src='a.gif' alt='blah'
style='blah'>");
var X=document.getElementById('picture1');
or
var X=document.images['picture1'];

Rest of script............

In Opera 6, X was undefined. However, if put into an 'initialize'
function then X returns as expected - [object - whatever].

I've decided not to bother with NN4 but I don't want to cause a page
freeze should someone still have it.

I agree with your last point but in this case, the script is the only
content.

Thanx for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top