Detection Of Real <Body> In Conditional Comments

I "borrowed" some code from the Khan Academy, which spits out a body tag with classes unique to IE versions.




<!--[if lt IE 7]>  <body class="ie ie6 lte9 lte8 lte7"> <![endif]-->

<!--[if IE 7]>     <body class="ie ie7 lte9 lte8 lte7"> <![endif]-->

<!--[if IE 8]>     <body class="ie ie8 lte9 lte8"> <![endif]-->

<!--[if IE 9]>     <body class="ie ie9 lte9"> <![endif]-->

<!--[if gt IE 9]>  <body> <![endif]-->

<!--[if !IE]><!-->

<body>

<!--<![endif]-->

Thing is, when registering client scripts at the beginning of the body, Yii goes for the first body tag instead of the right body tag.


Yii::app()->clientScript->registerScript('myScript','// ...',CClientScript::POS_BEGIN)

Yields this in non-IE browsers:




<!--[if lt IE 7]>  <body class="ie ie6 lte9 lte8 lte7"><script type="text/javascript">

/*<![CDATA[*/

// ...

/*]]>*/

</script>

 <![endif]-->

<!--[if IE 7]>     <body class="ie ie7 lte9 lte8 lte7"> <![endif]-->

<!--[if IE 8]>     <body class="ie ie8 lte9 lte8"> <![endif]-->

<!--[if IE 9]>     <body class="ie ie9 lte9"> <![endif]-->

<!--[if gt IE 9]>  <body> <![endif]-->

<!--[if !IE]><!-->

<body>

<!--<![endif]-->

My request is for having Yii detect the right body tag, since this would mean I would not need to grab an extension to address this issue.

Can you not use a div wrapper with the ie class?




<body>

  <div class="ie">

    ...

  </div>

</body>



Looks like a bug in Yii - could you perhaps create a ticket? :)

Yii shouldn’t grab a tag which is commented out.

Would there be the same problem even if the script would be inserted under the last body?

I mean, in your example above, the last <body> is inside an IF structure… so the scripts would be inserted inside that IF too, and would there be a problem with IE 7,8,9 as they would not use that IF at all?

Interesting issue if I may say. But can you tell your use case? I can’t see a practical case of a script that should be registered in the beginning of the body, that can’t be registered in the head itself (ie POS_HEAD instead of POS_BEGIN).

Using this trick, we could write IE specific hacks in CSS …




body {

   ...

}

body.ie6 {

   ...

}

body.ie7 {

   ...

}



But as Maurizio says, Yii can not tell which <body> will be adopted by the user browser …

So I think including an IE version specific css with this trick would be a decent solution.




<!--[if lt IE 7]>  <link rel='stylesheet' type='text/css' href='ie6.css'> <![endif]-->

<!--[if IE 7]>     <link rel='stylesheet' type='text/css' href='ie7.css'> <![endif]-->

<!--[if IE 8]>     <link rel='stylesheet' type='text/css' href='ie8.css'> <![endif]-->

<!--[if IE 9]>     <link rel='stylesheet' type='text/css' href='ie9.css'> <![endif]-->