rtlm has asked for the wisdom of the Perl Monks concerning the following question:

Gday monks
i am having compatability issues with my website between IE and Netscape.
What i need to do is check to see which browser a user has, and if it is Netscape, have Perl alter the HTML a little and then print out the page.
I have considered using Javascript when the page loads up to detect which browser the user has, and if it is Netscape, having it call a Perl program which will open the HTML file, alter it, and then print it out.
The problem with this is that i don't know how to have the Perl program know which file to open, as i intend to have this check done for every page in my site. How can I do this?
Thanks Guys.

Thanks for the replies, i guess your right in that its not worth going to all the trouble. The problem i am having is for some reason in NS and Opera everything on my page appears up about 10 or so pixels down. i have no idea why this is so i was just thinking i could use Perl to insert a 10 pixel or so spacer gif to counteract this if the browser is not IE. I'm sure there is another way to fix it, it's just that i am quite new to Perl and am still marvelling at its capabilities :)

if you want to check out what i mean go to http://www.hydrocool.com, and view the page in both IE and NS.

Replies are listed 'Best First'.
Re: html alteration
by ViceRaid (Chaplain) on Jul 19, 2004 at 10:59 UTC

    You can use the suggestions you've got above to do this. However, if the HTML is under your control, I'd strongly suggest you look at fixing it so that it works consistently between IE and NN. This really shouldn't be that hard (unless you're a masochist and trying to support very old v4 browsers), especially if you get browsers to parse your page in 'Strict' mode by using the appropriate DTD.

    Keep It Simple: the more things that have to happen from a browser requesting a page to the server sending it back, the more things that can go wrong. Serving flat HTML files is simple. Having client-side code call a CGI, rewrite an (arbitrary?) file is fiddly, fragile, harder to maintain, consumes more server resources and slower.

    If you really can't manage to get consistency using HTML & CSS, see if you can at least handle the inconsistency purely on the client-side using Javascript.

    Cheers
    ViceRaid

      I couldn't agree more. In fact, I'd like to see your HTML to see what you are talking about (maybe outside of the Monastery walls). I spend much of my time getting web pages to work across browsers (especially since moving to all CSS-tableless-XHTML). I'm sure there's a way to solve your problem on the client side (again, provided you aren't trying to do something crazy like make it work in <6.0 NS).

      —Brad
      "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton

      Hear, hear. See also Designing With Web Standards (ISBN ISBN 0735712018) for why browser-dependent hacks are evil.

Re: html alteration
by ccn (Vicar) on Jul 19, 2004 at 09:12 UTC
Re: html alteration
by borisz (Canon) on Jul 19, 2004 at 09:52 UTC
    Look at the 'User-Agent' header. Most Browsers send it to you.
    Boris
Re: html alteration
by pg (Canon) on Jul 19, 2004 at 13:24 UTC

    There are two different aspects here:

    • For HTML tags, you should only use those standard W3C HTML or XHTML tags (not those obsoleted ones), not tags that are only defined and supported by particular browsers. There is really no point to waste time, and deal with them. None of them makes your web page much better.
    • For JavaScript, deal with the differences on client side, let your JavaScript handle it. JavaScript can detect the browser type, and for different types, you might need different syntax (this is life), and also some browser do not support certain things.

    In general, it is not a good idea to send out different pages from server side, simply for different browsers.

    Update:

    Thanks for ViceRaid to follow up and elaborate on my thoughts.

      For JavaScript, deal with the differences on client side, let your JavaScript handle it. JavaScript can detect the browser type, and for different types, you might need different syntax (this is life), and also some browser do not support certain things.

      This is getting OT, but when doing compatibility coding for client-side Javascript, it's generally a better idea to test for the availability of the given methods or variables you want to use, rather than parsing the UA string (on the server-side, you don't have this luxury). This, by the by, is another reason to do client-compatability coding on the client-side. Anyway, here's an (untested) example:

      var foo; if ( document.getElementById ) { foo = document.getElementById('foo'); } elsif ( document.all ) { foo = document.all['foo']; } ...

      One of the biggest advantages of this is that it much improves forward compatibility (it should still work when Mozilla v3.0 arrives). It also works when you have a browser that has had its UA-string mangled by a vendor, but is otherwise just a vanilla version of a well-known browser engine (IE, Gecko, whatever).