1

I included some jQuery script on my site. The script doesn't work with anything lower than IE7. Now I want to do the following:

<!--[if !(lt IE 7]>
    <?php include 'script.html'; ?>
<![endif]-->

It should only load the script if the browser is "not lower than IE7". But it doesn't work.

Is there any way to solve this without using a PHP based browser recognition?

0

6 Answers 6

5

Yes, build the browser recognition into your jQuery/javascript. In the javascript, test for capability or object you're needing, and if it's not there, then have code to deal with it.

Sign up to request clarification or add additional context in comments.

1 Comment

I would also recommend this. :)
0

could be the opening ( that doesn't have a closing ) ?

Comments

0

Help with the first part: it doesn't work because you don't need the ( before lte, nor the !. Use

<!--[if gte IE 7]>
  <?php include 'script.html'; ?>
<![endif]-->

Further reading: Microsoft's page on IE conditional comments

5 Comments

He's asking for a non-PHP solution.
fair enough. he did also point out that his code didn't work. i defer to your answer.
@Jonathan M OP said non PHP browser recognition solution. This isn't - it's IEs' in-built conditional comments.
Conditional statements only work after the page has been sent to the browser so PHP include files will not work
@Rory, yes, you're correct about the conditional, but the alternative javascript is still being included (incorrectly) by PHP. I think that's what the OP was trying to get away from.
0

another alternative can be to use the well known ie7.js script that makes older IE versions more "standard" compliant that would surely make your jQuery script work.

you can find it here

Comments

0

You shouldn't be using browser detection. Instead, use Feature Detection to test if your browser is capable of something you want to do.

Comments

0

in php you would do one of the following

$browser = get_browser(null, true);
if($browser['browser'] == 'MSIE' && $browser['version'] >= 7) { /*CODE*/

OR

$u_agent = $_SERVER['HTTP_USER_AGENT']; 
$bname = 'Unknown';
$version= "";

if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
{ 
    $bname = 'Internet Explorer'; 
} 

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
    // we have no matching number just continue
}

The IE Conditional statements only work after the page has been sent to the browser so PHP include files will not work, To include a JS script file you would use

<!--[if !(lt IE 7]>
<script src='script.js' type='text/javascript'></script>
<![endif]-->

For a javascript browser detection you would use:

if(BrowserDetect.browser == "MSIE" && BrowserDetect.version >= '7') {
   /* CODE */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.