views:

79

answers:

2

Hello everyone, how can I get the name of the browser ( I'm interested in getting IE7 because it creates problems for me) by using javascript?

Reason why I want a browser name so I can assign class to one of my footer elements which doesn't look good in IE7.

I want to do this on page load, #1 check browser name , 2# if IE 7 assign class to element else do nothing. Is this the right approach by the way ? thank you

UPDATE

I have HTML something like this

<div id="some_div">
 some content
</div>

Can I use IE conditional comments and how? obviously if I put another div in front or below this div which appears only in IE both divs will appear in IE7 then.

+4  A: 

Don't do this with JavaScript, do it with a conditional comment:

<!--[if IE 7]>
 ... some code only for IE 7
<![endif]-->

While you can't really alter your page structure in such conditional comments the canonical way to use them is simply to include an additional stylesheet with fixes in your <head>:

#footer {
   /* fixes here */
}
Joey
ok let me update the question
Gandalf StormCrow
+1 for mentioning CSS rules only for IE.
Boldewyn
So you suggest following approach, attach another style sheet which will be read only if IE7 is the browser. Inside it I should have some class footerie7, and I should add this class to existing element. And all in all the footerie7 class will not have effect on other browsers except for IE7?
Gandalf StormCrow
Boldewyn: I'd love other browsers having similar constructs. While not needed in an ideal world, the Gecko/Presto/WebKit world isn't ideal either and if something breaks in one of them you'll have to resort to pretty ugly tricks :/
Joey
Exactly, if I may speak for Johannes. This is a well-proven way and is suggested as fix for IE's CSS bugs by dozens of CSS gurus.
Boldewyn
Gandalf: Sorry, removed that part as it isn't necessary. The main point is: Include all your normal styles, then, as the last style you include make it the IE 7 fix stylesheet in which you can override whatever styles were set before. the additional class I mentioned isn't even necessary, sorry. (But a class without styles applied to it doesn't do anything, to answer your question).
Joey
@Johannes: Me, too. Just the other day I looked for a 'conditional statement' for FF and all I could come up with was the `@-moz-document{}` @-rule and that's not the same.
Boldewyn
hmm one more thing .. can comments be inside <style> tags?
Gandalf StormCrow
No. Conditional comments are for use inside HTML. Within style tags you have CSS. There is no way of getting CCs inside of `<style>`. However, you can wrap CCs around `<style>`
Joey
Well that is the main problem why I decided to use javascript instead of comments, cause I'm using jsp and everything is predefined in taglibs .. I can't just say I'll paste this here and it will be rendered, and plus I just pitched in to help so its really hard to get arround someone else's code (epsecially if that someone is not there to show you :) .. anymore suggestions?
Gandalf StormCrow
+3  A: 
<!--[if IE 7]>
<script type="text/javascript">IE7=true;</script>
<![endif]-->
<script type="text/javascript">
if (IE7) {
  /* do fancy error correction here... */
}
</script>

Another link about conditional comments.

Boldewyn