test for ie or netscape

T

Treetop

I have seen some codes that can test for the browser and give values
accordingly. I tried to read the FAQ, but was unable to find a simple
version of this. What I want is

If Netscape-test {
code for netscape users
}

If IE-test {
code for ie users
}
 
M

Michael Winter

I have seen some codes that can test for the browser and give values
accordingly. I tried to read the FAQ, but was unable to find a simple
version of this.

Why do you want to do this? If you are trying to determine what code *can*
be executed, use feature detection, not browser detection. For example, to
see if document.getElementById() is supported use

if( document.getElementById ) {
}

Mike
 
T

Treetop

I have created a script to send data, text and an image src, to an
iframe. I have code that works for IE, and I have code that works for
Netscape, but I can't find code that works for both. I want the
following

if test-for-netscape {
window.frames['external'].setup(links[aNum]);
}
else {
document.external.setup(links[aNum]);
}


Michael Winter said:
Why do you want to do this? If you are trying to determine what code *can*
be executed, use feature detection, not browser detection. For example, to
see if document.getElementById() is supported use

if( document.getElementById ) {
}

Mike
reply)
 
K

kaeli

I have seen some codes that can test for the browser and give values
accordingly. I tried to read the FAQ, but was unable to find a simple
version of this. What I want is

And if I use Opera? Or Safari? Or Konqueror? Mac users tend to like
Safari. What about Mozilla? Linux users tend to like Mozilla.
What about if I use Netscape 4? It isn't anything close to Netscape 6 in
regards to what it can do.
How about if I use IE3? It doesn't support half the stuff IE5 does.

Don't test for browsers. You'll end up coming back here wondering why
your code doesn't work in Netscape 8 when it comes out or something.
Test for the objects you want to use.
if (document.layers)
// do something with that for NN4
else if (document.all && ! document.getElementById)
// do something with old IE
else if (document.getElementById)
// recent browsers like IE5+, NN6+, Mozilla, Opera...


--
 
T

Treetop

kaeli said:
And if I use Opera? Or Safari? Or Konqueror? Mac users tend to like
Safari. What about Mozilla? Linux users tend to like Mozilla.
What about if I use Netscape 4? It isn't anything close to Netscape 6 in
regards to what it can do.
How about if I use IE3? It doesn't support half the stuff IE5 does.

Don't test for browsers. You'll end up coming back here wondering why
your code doesn't work in Netscape 8 when it comes out or something.
Test for the objects you want to use.
if (document.layers)
// do something with that for NN4
else if (document.all && ! document.getElementById)
// do something with old IE
else if (document.getElementById)
// recent browsers like IE5+, NN6+, Mozilla, Opera...

I need to have a way to test for IE5+ versus NN6+.


For my code to work for NN6 I need the following:

window.frames['external'].setup(links[aNum]);


For IE6 I need the following:

document.external.setup(links[aNum]);
 
K

kaeli

I need to have a way to test for IE5+ versus NN6+.

I bet you don't.
For my code to work for NN6 I need the following:

window.frames['external'].setup(links[aNum]);


For IE6 I need the following:

document.external.setup(links[aNum]);

That makes no sense to me without a context.

Are you trying to call a function resident (defined) in another frame of
a frameset?
If so, the following is mine (well, it's HVMenu's really) and it works
on both IE5+ and NN6+.
It is called from the "main" frame of a 3 frame (top, left, main)
layout. It calls a function defined in the 'left' frame from the 'main'
frame.

if(parent.frames[0]&&parent.frames['leftFrame'].Go)
{
parent.frames['leftFrame'].Go();
}

If that's the kind of thing you're trying, tell me more about your frame
layout and what frame is calling what function and what frame name that
function is in.

--
 
M

Michael Winter

I have created a script to send data, text and an image src, to an
iframe. I have code that works for IE, and I have code that works for
Netscape, but I can't find code that works for both. I want the
following

if test-for-netscape {
window.frames['external'].setup(links[aNum]);
}
else {
document.external.setup(links[aNum]);
}

I don't know if this will work, but give it a shot:

var ifExternal = null;

if( window.frames['external'] ) {
ifExternal = window.frames['external'];
} else if( document.external ) {
ifExternal = window.external;
}

if( ifExternal ) {
ifExternal.setup( links[ aNum ]);
}

The need for the two different code paths seems to be because IE (and
Opera) don't include IFRAMEs in the frames collection, though they do
support the frames collection in general (or seem to).

Mike
 
B

Bas Cost Budde

Dennis said:
and my reply is:
Do a Google search on "browser sniffer"

Maybe; but the discussion as I perceive it tends towards object
detection, not browser sniffing.

Er, I don't want to start a war here, if this is still opiniated matter,
I'm out.
 
B

Bas Cost Budde

I need to have a way to test for IE5+ versus NN6+.
I bet you don't.

Maybe this should/could read: I know a way for IE5, and I know a
(different) way for NN6, and I'm looking for any reliable way to
distinguish between the two possibilities.

I, personally, wouldn't want to drop in the gap when both tests for IE5
and NN6 fail. That convinced me to use object detection, not browser
sniffing.
 
T

Treetop

OK he it goes...... Some of my clients are local contractors /
construction companies. They like to have images to show off current
and past projects. Most of the time it involves an image and some
text to explain what picture is. The last update I did was over 70
images. I decided to have a blank page that could be updated with a
link, that would put the source for the image and text for the image
on that page, to save me time creating 70 pages. I was unable to find
a way and had to create 70 pages. Not fun.

Today I finally put enough information together from different sources
to create the following code in two pages. I don't like they way I
trick the system into using the correct code. I also don't have a way
to test other browsers other than IE and Netscape. This code works on
my sytem in both IE 6 and Netscape 7.02.



Netscape uses this code:
window.frames['external'].setup(links[aNum]);}

IE uses this code:
document.external.setup(links[aNum]);





index page:

<table border="1" width="600">
<tr valign="top">
<td width="200">

<script language="javascript" type="text/javascript">

var links=new Array();
links[4]=new Array("pic1.jpg","pic1 link text","text for pic1");
links[3]=new Array("pic2.jpg","pic2 link text","text for pic2");
links[2]=new Array("pic3.jpg","pic3 link text","text for pic3");
links[1]=new Array("pic4.jpg","pic4 link text","text for pic4");

function showImg(aNum)
{
if (navigator.appName.substring(0, 7) != "Microso"){
window.frames['external'].setup(links[aNum]);}
else {
document.external.setup(links[aNum]);
}
};


for (var i=links.length-1;i>=0;i--)
{
document.write('<a href="#" onclick=showImg("'+i+'")>' + links[1] +
'</a><br>');
}

</script>

</td>
<td align="center" width="500">
<iframe name="external" id="external" style="width:100%;height:500px"
src="picture.html"></iframe>
</td></tr></table>




here is the code in the pictrure.html file

<script language="javascript">
function setup(aLink)
{
document.getElementById("pic").src=aLink[0];
document.getElementById("title").innerHTML=aLink[2];
}
</script>
</head>
<body>

<center>
<img src="pic1.jpg" id="pic">
<p id="title">Text for Pic1</p>
</center>
 
T

Treetop

OK he it goes...... Some of my clients are local contractors /
construction companies. They like to have images to show off current
and past projects. Most of the time it involves an image and some
text to explain what picture is. The last update I did was over 70
images. I decided to have a blank page that could be updated with a
link, that would put the source for the image and text for the image
on that page, to save me time creating 70 pages. I was unable to find
a way and had to create 70 pages. Not fun.

Today I finally put enough information together from different sources
to create the following code in two pages. I don't like they way I
trick the system into using the correct code. I also don't have a way
to test other browsers other than IE and Netscape. This code works on
my sytem in both IE 6 and Netscape 7.02.



Netscape uses this code:
window.frames['external'].setup(links[aNum]);}

IE uses this code:
document.external.setup(links[aNum]);





index page:

<table border="1" width="600">
<tr valign="top">
<td width="200">

<script language="javascript" type="text/javascript">

var links=new Array();
links[4]=new Array("pic1.jpg","pic1 link text","text for pic1");
links[3]=new Array("pic2.jpg","pic2 link text","text for pic2");
links[2]=new Array("pic3.jpg","pic3 link text","text for pic3");
links[1]=new Array("pic4.jpg","pic4 link text","text for pic4");

function showImg(aNum)
{
if (navigator.appName.substring(0, 7) != "Microso"){
window.frames['external'].setup(links[aNum]);}
else {
document.external.setup(links[aNum]);
}
};


for (var i=links.length-1;i>=0;i--)
{
document.write('<a href="#" onclick=showImg("'+i+'")>' + links[1] +
'</a><br>');
}

</script>

</td>
<td align="center" width="500">
<iframe name="external" id="external" style="width:100%;height:500px"
src="picture.html"></iframe>
</td></tr></table>




here is the code in the pictrure.html file

<script language="javascript">
function setup(aLink)
{
document.getElementById("pic").src=aLink[0];
document.getElementById("title").innerHTML=aLink[2];
}
</script>
</head>
<body>

<center>
<img src="pic1.jpg" id="pic">
<p id="title">Text for Pic1</p>
</center>
 
R

Richard Cornford

The need for the two different code paths seems to be because
IE (and Opera) don't include IFRAMEs in the frames collection,
though they do support the frames collection in general
(or seem to).

IE and Opera do make IFRAMEs members of the frames collection. By
integer index and NAME attribute, ID attributes are more problematic and
for cross-browser scripting NAME attributes are preferred, though they
may be doubled up with corresponding ID attributes if needed.

Richard.
 
T

Treetop

Bas Cost Budde said:
Maybe this should/could read: I know a way for IE5, and I know a
(different) way for NN6, and I'm looking for any reliable way to
distinguish between the two possibilities.

I, personally, wouldn't want to drop in the gap when both tests for IE5
and NN6 fail. That convinced me to use object detection, not browser
sniffing.

What do you recommend I use then to make this work? If you have an
example of code would be wonderful.
 
T

Treetop

Richard Cornford said:
IE and Opera do make IFRAMEs members of the frames collection. By
integer index and NAME attribute, ID attributes are more problematic and
for cross-browser scripting NAME attributes are preferred, though they
may be doubled up with corresponding ID attributes if needed.

Richard.

What do you recommend I use then to make this work? If you have an
example of code would be wonderful.
 
M

Michael Winter

IE and Opera do make IFRAMEs members of the frames collection. By
integer index and NAME attribute, ID attributes are more problematic and
for cross-browser scripting NAME attributes are preferred, though they
may be doubled up with corresponding ID attributes if needed.

A quick test seemed to indicate that numeric indexes work with IFRAMEs
(forgot to mention that), but NAMEs didn't. Of course now, both work. The
test is the same as before, but re-written so I'm puzzled.

Mike
 
R

Richard Cornford

Maybe; but the discussion as I perceive it tends
towards object detection, not browser sniffing.

Er, I don't want to start a war here, if this is still
opiniated matter, I'm out.

It is not a matter of opinion, there are no browser detecting techniques
that can reliably tell you whether a browser is, for example, a version
of a Netscape or a Microsoft browser. The non-Microsoft and Netscape
browsers put so much effort into lying about it in their UA strings
and/or reproducing (or faking) the features of more common browsers that
you cannot tell them apart.

Fortunately feature detecting as a technique has no interest in the type
or version of the browser in use so it stops being important that you
cannot tell which browser it is.

Richard.
 
K

Kevin Scholl

Treetop said:
OK he it goes...... Some of my clients are local contractors /
construction companies. They like to have images to show off current
and past projects. Most of the time it involves an image and some
text to explain what picture is. The last update I did was over 70
images. I decided to have a blank page that could be updated with a
link, that would put the source for the image and text for the image
on that page, to save me time creating 70 pages. I was unable to find
a way and had to create 70 pages. Not fun.

The ideal solution would be server-side. But if you cannot do that for
whatever reason, there are client-side solutions, though they require
Javascript (which doesn't seem to be an issue for you since that's the
tract you are taking anyway). It sounds like you could benefit from
searching the substring in a link, as I've done in the photo gallery of
my softball site:

http://www.wrayvensoftball.com/global/gallery.shtml

Clicking on a thumb invokes a single photo page, which in turn reads the
substring following the ? in the link to determine what image and
caption to display. The images and captions themselves are in an aray in
a simple text file.

It's not too difficult to figure out by looking at the source code of
the various files involved.

--

*** Remove the DELETE from my address to reply ***

======================================================
Kevin Scholl http://www.ksscholl.com/
(e-mail address removed)
 
R

Richard Cornford

What do you recommend I use then to make this work? ...
<snip>

if((window.frames)&&( window.frames['iframeName'])&&
(window.frames['iframeName'].setup)){
window.frames['iframeName'].setup(links[aNum]);
}

-and change the name of the IFRAME so that it is not using the same name
as any existing properties of the window object (such as "external") on
any browsers.

Richard.
 
R

Richard Cornford

A quick test seemed to indicate that numeric indexes work
with IFRAMEs (forgot to mention that), but NAMEs didn't. Of
course now, both work. The test is the same as before, but
re-written so I'm puzzled.

I was surprised that you didn't find the IFRAME in the frames collection
under its NAME attribute (IDs don't work with Opera <= 6 but should with
later versions). Still I am glad you are finding them now as it means
that the cross-browser approach of accessing named IFRAMEs only as named
members of the frames collection is still valid. I was sure IE 6 hadn't
changed but I haven't got round to installing the latest versions of
Opera 7 yet and they might have changed something.

I think that the OP is possibly shooting himself in the foot a bit using
the name "external" for the IFRAME as IE already has a global property
called "external" (and other browsers may also implement, or spoof, it)
and on many browsers, including IE, the frames collection is a reference
back to the global object anyway.

Richard.
 
B

Bas Cost Budde

Treetop said:
What do you recommend I use then to make this work? If you have an
example of code would be wonderful.

It is almost in your original question.


if (window.frames) {
window.frames['external'].setup(links[aNum]);
} else if (document.external) {
document.external.setup(links[aNum]);
}

Maybe add another if for the case both expressions fail. But as long as
you remain within IE5 and NN6 one of them will succeed, so no need.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top