Det var så ikke skrevet helt rigtigt - sådan er det når man skriver fra hukommelsen...
<!--[if IE]>You are using Internet Explorer<![endif]-->
Uanset hvad, så er conditional comment oplagt hvis det netop er IE du skal have fat i. Desværre er der ikke andre browsere der kan se det smarte i sådan en ting, så Firefox, m.v. implementerer ikke deres egne conditionals.
Du har fint mulighed for at tilføje en else (der er nægtende conditional comments, som dermed kun vises i andre browsere end IE, omend det ikke går gennem W3C).
Nu er det jo desuden netop IE spørgeren vil have, og hvis det, som vi må gå ud fra med de oplysninger vi har, udelukkende er et spørgsmål om IE/ikke-IE, er conditional comments til enhver tid overlegen over hacks, user-agent check, m.v., netop fordi det ikke er noget der kan fuskes med.
<? function browser_detection( $which_test ) { /* sample:
$a_browser_data = browser_detection('full'); if ( $a_browser_data[0] !== 'msie' ) { execute the non msie conditions } else// if it is msie, that is { if ( $a_browser_data[1] >= 5 ) { execute the ie stuff } } */ // initialize variables $browser_name = ''; $browser_number = ''; // get userAgent string $browser_user_agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : ''; //pack browser array // values [0]= user agent identifier, lowercase, [1] = dom browser, [2] = shorthand for browser, $a_browser_types[] = array('opera', true, 'op' ); $a_browser_types[] = array('msie', true, 'ie' ); $a_browser_types[] = array('konqueror', true, 'konq' ); $a_browser_types[] = array('safari', true, 'saf' ); $a_browser_types[] = array('gecko', true, 'moz' ); $a_browser_types[] = array('mozilla/4', false, 'ns4' );
for ($i = 0; $i < count($a_browser_types); $i++) { $s_browser = $a_browser_types[$i][0]; $b_dom = $a_browser_types[$i][1]; $browser_name = $a_browser_types[$i][2]; // if the string identifier is found in the string if (stristr($browser_user_agent, $s_browser)) { // we are in this case actually searching for the 'rv' string, not the gecko string // this test will fail on Galeon, since it has no rv number. You can change this to // searching for 'gecko' if you want, that will return the release date of the browser if ( $browser_name == 'moz' ) { $s_browser = 'rv'; } $browser_number = browser_version( $browser_user_agent, $s_browser ); break; } } // which variable to return if ( $which_test == 'browser' ) { return $browser_name; } elseif ( $which_test == 'number' ) { return $browser_number; }
/* this returns both values, then you only have to call the function once, and get the information from the variable you have put it into when you called the function */ elseif ( $which_test == 'full' ) { $a_browser_info = array( $browser_name, $browser_number ); return $a_browser_info; } }
// function returns browser number or gecko rv number // this function is called by above function, no need to mess with it unless you want to add more features function browser_version( $browser_user_agent, $search_string ) { $string_length = 8;// this is the maximum length to search for a version number //initialize browser number, will return '' if not found $browser_number = '';
// which parameter is calling it determines what is returned $start_pos = strpos( $browser_user_agent, $search_string );
// start the substring slice 1 space after the search string $start_pos += strlen( $search_string ) + 1;
// slice out the largest piece that is numeric, going down to zero, if zero, function returns ''. for ( $i = $string_length; $i > 0 ; $i-- ) { // is numeric makes sure that the whole substring is a number if ( is_numeric( substr( $browser_user_agent, $start_pos, $i ) ) ) { $browser_number = substr( $browser_user_agent, $start_pos, $i ); break; } } return $browser_number; } ?>
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.