需要切换if / else

时间:2011-12-29 01:36:38

标签: jquery jquery-mobile if-statement external-script

我有一些代码用于在移动设备和jQuery以及普通计算机上的其他代码上显示jQuery mobile和其他一些代码。我听说最好把jQuery代码放在jQuery mobile之前,所以我想知道如何切换if / else语句,以便常规计算机代码是第一个。最好看到我的代码的编辑版本。

<?php $mobile_browser = '0'; if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda ','xda-'); if (in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) { $mobile_browser = 0; } if ($mobile_browser > 0) { ?>
    <link href="http://www.luvbid.com/css/mobilestyle.css" type="text/css" rel="stylesheet" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
    <script>
        $('div').bind('pageinit', function() {
            $(this).find('a').bind('click', function(e) {
                e.preventDefault();
                //add your custom ajax call here
            });
        });

    </script>
    <? } else { ?>
    <link href="http://www.luvbid.com/css/style.css" type="text/css" rel="stylesheet" />
    <script src="http://cdn.jquerytools.org/1.2.6/jquery.tools.min.js"></script>
    <link type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/smoothness/jquery-ui.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
    <script src="js/jquery.alerts.js"></script>
    <script src="http://cdn.jquerytools.org/1.2.6/form/jquery.tools.min.js"></script>
    <? } ?>

3 个答案:

答案 0 :(得分:1)

将第一行代码的最后一部分更改为

if ($mobile_browser <= 0)

而不是

if ($mobile_browser > 0)

你应该做生意。请记住移动所有包含和内容:)

答案 1 :(得分:0)

 if ($mobile_browser > 0)...

变为

 if ($mobile_browser <= 0)...

但这并不会对您有所帮助,因为只有在评估表达式后才会输出引用移动版本的代码。这种情况发生在服务器端,与客户角度的页面性能无关。

答案 2 :(得分:0)

目前,第一行中的所有陈述都分开了,看起来像这样:

<?php $mobile_browser = '0'; 
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } 
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } 
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)); 
$mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda ','xda-'); if (in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } 
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) { $mobile_browser++; } 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) { $mobile_browser = 0; } 
if ($mobile_browser > 0) { ?>

将最终的if语句更改为:

if ($mobile_browser == 0)