PDA

View Full Version : Redirect to Opera script?



edman
05-05-2006, 07:41 AM
I've seen a lot of browser redirect scripts but I haven't seen one that redirects to Opera...can somebody post one?

djr33
05-05-2006, 07:49 AM
<html>
<head>
...
<meta http-equiv="refresh" content="5;url=http://google.com">
...
</head>
<body>...</body></html>


The 5 means 5 seconds... use what you want, 0 for immediate.
The ;url= part is optional. note that it doesn't require quotes around the url itself, but only the content="" value as a whole.

edman
05-08-2006, 12:42 AM
Thanx, that script will come in handy. But that isn't quite what I meant. I meant, say you create a webpage and you have three different pages, one for IE, one for Firefox, and one for Opera, and you want your index.html file to be a script that will determine what browser the user is using so that you can direct him/her to the either the IE, Firefox, or Opera page. Something like this:
if(navigator.userAgent.indexOf("Firefox") != -1)
{
window.location = "index2.htm";
}
else if(navigator.userAgent.indexOf("MSIE") != -1)
{
window.location = "index1.htm";
}

But how do you write Opera? Will this work...

else if(navigator.userAgent.indexOf("Opera") != -1)
{
window.location = "index3.htm";
}


?????????

Thanx,
Edman

djr33
05-08-2006, 01:10 AM
Oh. Right.

Hmm... not really sure. For IE, there are weird comment-based if statements you can use.

Something like:
<!-- [IF IE] <a href="blah.htm">click</a> ELSE --> <a href....></a> <!--....


Or, just plain JS.

But i'm not sure how to do that with JS.

I think that "if document.all" checks, because only IE has that compatibility.

But... not sure about mozilla vs. opera, etc.


There are ways, I'm sure.

But can't really help you.

Random note-- there's a way to check with PHP what the browser is (more compatible/more complex than JS) and then you could use that. JS might suit your needs, though.

otaku
05-08-2006, 01:21 AM
I've seen a lot of browser redirect scripts but I haven't seen one that redirects to Opera...can somebody post one?

window.opera will do the job:


if(window.opera){
...
}

djr33
05-08-2006, 01:27 AM
Hmm... maybe. Does that work for all browsers?
I wouldn't think it's that simple, but I don't know. Kinda seems familiar, though.

otaku
05-08-2006, 02:09 AM
My suggestion is to use object detection like below:



<script type="text/javascript">

var ns4 = (document.layers);
var ns6 = (!document.all && document.getElementById);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById && !window.opera);
var op6 = (window.opera && document.getElementById);

function objDetection(){
if(ns4){
alert('Netscape 4');
}
if(ns6){
alert('Mozilla / Firefox / Netscape 6 and more');
}
if(ie4){
alert('Explorer 4');
}
if(ie5){
alert('Explorer 5 and more');
}
if(op6){
alert('Opera 6 and more');
}
}
onload=objDetection;

</script>

djr33
05-08-2006, 02:21 AM
onload=objDetection;

That doesn't need to be in the body tag?
Nice :)

Very good script from the looks of it.

jscheuer1
05-08-2006, 04:25 AM
You don't really need an onload function or all those brackets (red):


<script type="text/javascript">

var ns4 = (document.layers);
var ns6 = (!document.all && document.getElementById);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById && !window.opera);
var op6 = (window.opera && document.getElementById);


if(ns4){
alert('Netscape 4');
}
if(ns6){
alert('Mozilla / Firefox / Netscape 6 and more');
}
if(ie4){
alert('Explorer 4');
}
if(ie5){
alert('Explorer 5 and more');
}
if(op6){
alert('Opera 6 and more');
}


</script>

But, the brackets would come in handy if you wanted to do more than one thing in each instance. Onload would also be good if you wanted to act upon the loaded document instead of merely fire an alert. You are still treading on dangerous ground here. Not you personally, otaku but, the OP. You are better off designing your page to work in all your target browsers rather than branch off to separate pages. The way the above code equates object detection with certain browsers is accurate but, it is better to write one page with one script that uses object detection when and only when the script needs to use the object. That way any browser that supports the object can use it. We really don't need to know what browser the user has, just whether or not it supports the code. Another way of looking at this is, just because a browser passes this test:


var ie5 = (document.all && document.getElementById && !window.opera);

is no guarantee that it is IE5+. It may do/be all those things yet not support object.filters. IE5 itself is very limited in its support of that object while IE5.5+'s support for it is quite robust and IE6's even more so. This is just one example of why the object or method you want to use should be tested at the time you use it.

mwinter
05-08-2006, 09:33 AM
I meant, say you create a webpage and you have three different pages, one for IE, one for Firefox, and one for Opera [...]You would be insane. Not only is browser detection flawed and unreliable (despite claims to the contrary), authoring multiple versions of a document for different user agents is a complete waste of effort.

If you are having trouble with how a document is rendered, your implementation is broken. Trying to serve different versions of it is not the way forward. You should be attempting to determine why it happens and fix the problem, not avoid it.



var ns4 = (document.layers);Netscape 4, Escape, and OmniWeb.


var ns6 = (!document.all && document.getElementById);Netscape 6, iConnector, NetBox, Operas 5 & 6 (when not spoofing IE), Mozilla, K-meleon, Gostzilla, Doczilla, Firefox and other Gecko based browsers.


var ie4 = (document.all && !document.getElementById);Internet Explorer 4 and OmniWeb.


var ie5 = (document.all && document.getElementById && !window.opera);Internet Explorer 5+, Konquerer, Safari, NetFront, Web Browser, iCab, and IceBrowser.

That is not an exhaustive list.

Utter junk, as usual.

Mike

edman
05-08-2006, 12:36 PM
Thanx otaku for your script. But yes, that brings me to my second question: since Opera has the capability to 'pretend' that its IE, if it is in that "pretend I'm IE mode" would you even be able to detect its identity as Opera?

And my third and last question: Does anybody have any tips or any books to suggest or sites to go to that will help to understand how to make CSS layouts that look the same in all browsers. I always have the same problem: if it looks good in IE, something is wrong in Firefox, or if it looks good in firefox, something is wrong in IE...I heard that there is a way to use div tags to adjust the layout specifically for each browser but i dont know if thats true.
Maybe I will post this is the CSS forum as well.

Edman

mwinter
05-08-2006, 12:59 PM
Does anybody have any tips or any books to suggest or sites to go to that will help to understand how to make CSS layouts that look the same in all browsers.'The same in all browsers' is not a realistic aim. In fact, it's impossible, as all browsers will present at least some minor differences.

It is possible to have similar presentation as a goal (at least for graphical browsers), but that's about it.

It's best to start with well-written, valid markup using a Strict document type declaration (DOCTYPE). This will put modern browsers into Standards rendering mode[1], causing them to act in a much more predictable manner. Next, do you development with a good browser like Firefox and Opera. IE is broken in many ways, and authoring for it first will only increase the chances of you causing problems in better browsers. If necessary, you can always use conditional comments or hacks to present rules specifically to IE.

As for specific references, that's never been my forte. However, you might find Position is Everything (http://www.positioniseverything.net/) useful for identifying rendering bugs and finding information on how to work around them.


I heard that there is a way to use div tags to adjust the layout specifically for each browser but i dont know if thats true.That doesn't sound like a good idea.

The div element is a meaningless, block-level element. As it has no semantics of its own, it's often used when no other block-level element is appropriate, particularly for grouping parts of a document (headers, footers, navigation, etc.) together. However, neither it, nor any other element, should be used in the way you describe. One should also be careful with just how far one goes when using the div element. It's all too common to see people move from hard-to-maintain tag-soup containing deprecated elements and nested tables, to hard-to-maintain, semanticless div-soup containing div and span elements used all over the place instead of simpler, yet just effective, markup with the appropriate elements.

Mike


[1] Older browsers, like IE5.x, will always be less-than-capable. To get them to behave, should you choose to support them, you'll often have to resort to hacks.

otaku
05-08-2006, 02:11 PM
Hello edman,

Object detection is the best way to test a browser's capabilities. Here is a useful link:

http://developer.apple.com/internet/webcontent/objectdetection.html

You can also find on Google a lot of link that describe why Object detection is the best and secure way to detect browser's capabilities.

However use Object detection for javascript only, to perform an action depending which browser your visitor use.

For profesional website my suggestion is to make only 1 website version and focus on these following browsers:

Explorer 5+
Mozilla / Firefox / Netscape 6 and more
Opera 6 and more

That way you will get more then 98% of all people.

Most of the time you can easily fix your CSS layouts if you focus on these browsers.

otaku
05-08-2006, 04:23 PM
onload=objDetection;

That doesn't need to be in the body tag?
Nice :)

Very good script from the looks of it.


Hello djr33,

No it doesn't need to be in the body tag

jscheuer1
05-08-2006, 05:15 PM
Opera has the capability to 'pretend' that its IE, if it is in that "pretend I'm IE mode" would you even be able to detect its identity as Opera?

When Opera is pretending to be IE it still will return true to window.opera. The object 'window.opera' is, as I understand it, an extremely limited object. I've searched the net a few times on this and all I've been able to find on it is that it is an object of the Opera browser, period. It has no offsetHeight, name or title, for example, or any other properties I'm aware of. It was inserted into the Opera script parsing engine precisely to detect Opera.

OK then, how does Opera pretend to be IE? It changes its return value for this object:

navigator.userAgent

to mimic IE's.

Twey
05-08-2006, 05:47 PM
http://www.howtocreate.co.uk/operaStuff/operaObject.html

jscheuer1
05-08-2006, 06:21 PM
http://www.howtocreate.co.uk/operaStuff/operaObject.html

Oh, so it is good for something but, used to not be, you will notice that prior to v7, almost all are 'No support'.

otaku
05-08-2006, 06:31 PM
To detect Opera 6:
(window.opera && !document.createComment);

To detect Opera 7:
(window.opera && document.createComment);

jscheuer1
05-08-2006, 06:36 PM
To detect Opera 6:
(window.opera && !document.createComment);

To detect Opera 7:
(window.opera && document.createComment);

I want to take this opportunity to welcome you to these forums. Though I may differ with some of your opinions, I value your input here. I think I speak for others as well though this is often not expressed or considered much when posting. The culture here is often one of mutual critique, usually resulting in a whole greater, or at least equal to the sum of its parts.

Twey
05-08-2006, 07:09 PM
The culture here is often one of mutual critique, usually resulting in a whole greater, or at least equal to the sum of its parts.In other words, we'll probably pick massive holes in your code and maybe laugh at you occasionally, but it's all for the good of the code, and you shouldn't feel offended (or alone). :)
Warm welcome seconded.

otaku
05-08-2006, 07:59 PM
Thank you jscheuer1 and Twey for your welcome message, to be honest I did not have time for the mutual critique. I understand that you may differ with some of my opinions and that's correct. I did not take it personal.

I did not pay attention of all the critiques, you know when I come here and help people and someone else correct my code after me saying that I forgot the <title> tag, the brackets are not necessary if I have only 1 line of code in my "if" and other stuff like that. All these critique are correct and I did not reply to it because I presume that most of the programmers here know these things. I did not took that as a critique, I thought you gave these infomations to someone else.

When I am helping others, I presume they can add the <title> tag and choose there own DOCTYPE declaration. If they have questions on it, they will ask.

The question was how to detect Opera and my answer was window.opera. Yes my suggestion code was not "an exhaustive list" as mwinter said but the question was how to detect Opera and my code was something to start working with. That was an example how to use window.opera.

By the way I really don't care about Konquerer, NetFront, Web Browser, iCab, IceBrowser. I am trying to focus on IE5+, moz/firefox/netscape6+, sometime Opera6+.

I'm going to be really happy when VMware are going to release the new version with Mac as a guest operating system. That way I am going to be able to focus on Safari too.

Twey
05-08-2006, 08:24 PM
I'm going to be really happy when VMware are going to release the new version with Mac as a guest operating system. That way I am going to be able to focus on Safari too.QEmu (http://fabrice.bellard.free.fr/qemu/) supports PowerPC already.
By the way I really don't care about Konquerer, NetFront, Web Browser, iCab, IceBrowser.Mike wasn't pointing out all the tiny minorities, as such. It was more a case of pointing out that your code failed to deal with anything else that may pop up.

jscheuer1
05-08-2006, 08:28 PM
(most deleted for brevity) I'm going to be really happy when VMware are going to release the new version with Mac as a guest operating system. That way I am going to be able to focus on Safari too.

I like your attitude and a big thumbs up on that last sentiment. I'm going to do a search on VMware but, if you would, please supply a good link on this. As for these minor browsers, that's what designing to a decent browser, validating and using object detection are for, we can't realistically test in all those. Folks who use them must be used to the occasional or even frequent page(s) that just don't seem to work for them.

otaku
05-08-2006, 08:30 PM
Hello Twey,

Once again my code was an example how to use window.opera. Simply add an "else" at the end.



...

if(ie5){
alert('Explorer 5 and more');
}
if(op6){
alert('Opera 6 and more');
}
else{
alert('anything else');
}

thanks for the QEmu link, I will take a look later.

Twey
05-08-2006, 08:33 PM
Yes, I understand that it was only an example :)
VMWare homepage (http://www.vmware.com/), Wikipedia article (http://en.wikipedia.org/wiki/VMware).

otaku
05-08-2006, 08:51 PM
jscheuer1,



...please supply a good link on this...


I don't have any link in particular but if you search on their forum or any VMware groups, you are going to find some thread on it for sure. This has been discuss many time since Mac announced the use of Intel processors.

My co-worker just point me to a petition, I have just signed it!
http://www.petitiononline.com/vmwarosx/petition.html

Twey
05-08-2006, 08:54 PM
I still vastly prefer QEmu, since VMWare is not Free.

mwinter
05-09-2006, 03:53 PM
When I am helping others, I presume they can add the <title> tag and choose there own DOCTYPE declaration.It's nice to show faith in people, but from personal experience, I find it to misplaced when dealing with the finer points of markup. That is, if something looks like a complete example, it's not unusual to find it used as one, even if that wasn't the intention. For a little extra effort, I think it's generally better to ensure that posted code should either be fleshed out, or omissions should be noted. There can be no confusion, then.


The question was how to detect Opera and my answer was window.opera.However, sometimes people ask the wrong questions, and it's necessary to recognise that. Sometimes the right answer is "No", or "Don't even try it!". It may not be the answer the poster is looking for, but it serves them better than telling them how do something they shouldn't be doing.

The question here should have been, "I have rendering problems between browsers. How should I fix them?" Trying to detect a particular browser and serve different documents is not only likely to fail, but it doesn't fix anything. It just avoids the issue entirely. Moreover, it makes maintenance a nightmare. The notion shouldn't be entertained for even a moment.


Yes my suggestion code was not "an exhaustive list" [...]Which is why browser detection is flawed as a concept. There never will be an exhaustive list. No one person will ever know about all browsers, all versions, and the peculiarities of each one. But then again, one shouldn't care, anyway. That is, after all, the point of feature detection: to remove the need to know that level of detail.

The only time the browser is worth knowing is when encountering serious bugs in the host's scripting implementation or object model. However, these are so rare that they should never be a problem; I've never needed to deal with them.

Mike

jscheuer1
05-09-2006, 05:10 PM
I think it's generally better to ensure that posted code should either be fleshed out, or omissions should be noted. There can be no confusion, then.

You would be surprised, or perhaps not. Anyways, as I just recently saw in the NYT 'Science' section:


You can make something fool-proof but, you can't make something damn-fool-proof.

Twey
05-09-2006, 05:19 PM
I prefer
It's a constant battle: people build bigger and better fool-proof software, while the universe builds bigger and stupider fools.... for lack of a [paraphrase] tag :p