2011-04-30 78 views
4

因此,我正在研究我正在執行的網站的移動版本,並且迄今爲止我正在從主要網站(主要網站)拉動移動網站內容。「查看完整網站」移動網站選項

當我在那裏研究一些移動網站時,我注意到很多em有一個「查看完整網站」鏈接。

現在我計劃通過.js在主站點上的標題標記中通過檢查屏幕寬度等來重定向移動訪問者......(不知道它是否是迄今爲止最好的方法,但是我的大腦中最簡單)) (但建議也歡迎) 但像他這樣

if (screen.width<=XyZ||screen.height<=XyZ) //example iphone size lets say 320x480 
window.location.replace("mobile site link here.") 

同樣的東西我不知道這是否是最好的方式,但是,在虛擬測試,它適用於iPhone,一些朋友機器人,和一個黑莓。但它的工作。

反正,所以我的問題是,如果我在每一頁上都做這個檢查......我怎麼可能有一個「查看完整網站」選項?

+5

歡迎SO。下一次,請忽略所有'哈哈',需要幫助和問候;前兩個只是煩人的,最後一個是沒有必要的。我也喜歡[Alots](http://hyperboleandahalf.blogspot.com/2010/04/alot-is-better-than-you-at-everything.html)。 – 2011-04-30 22:18:35

+0

如果答案幫助你,請接受/投票,以便其他用戶可以輕鬆找到解決方案,他們絆倒同樣的問題 – neebz 2011-05-01 00:15:26

回答

0

這不是一個最好的方法,因爲很多時候JS並不支持移動瀏覽器。

您可以使用此功能:

function its_mobile_browser($user_agent = '') 
{ 
    if (empty($user_agent)) 
    { 
     $user_agent = $_SERVER['HTTP_USER_AGENT']; 
     if (empty($user_agent)) return false; 
    } 

    if (stripos($user_agent, 'Explorer')!==false || 
     stripos($user_agent, 'Windows')!==false || 
     stripos($user_agent, 'Win NT')!==false || 
     stripos($user_agent, 'FireFox')!==false || 
     stripos($user_agent, 'linux')!==false || 
     stripos($user_agent, 'unix')!==false || 
     stripos($user_agent, 'Macintosh')!==false 
    ) 
    { 
     if (!(stripos($user_agent, 'Opera Mini')!==false 
       || stripos($user_agent, 'WAP')!==false 
       || stripos($user_agent, 'Mobile')!==false 
       || stripos($user_agent, 'Symbian')!==false 
       || stripos($user_agent, 'NetFront')!==false 
       || stripos($user_agent, ' PPC')!==false 
       || stripos($user_agent, 'iPhone')!==false 
       || stripos($user_agent, 'Android')!==false 
       || stripos($user_agent, 'Nokia')!==false 
       || stripos($user_agent, 'Samsung')!==false 
       || stripos($user_agent, 'SonyEricsson')!==false 
       || stripos($user_agent, 'LG')!==false 
       || stripos($user_agent, 'Obigo')!==false 
       || stripos($user_agent, 'SEC-SGHX')!==false 
       || stripos($user_agent, 'Fly')!==false 
       || stripos($user_agent, 'MOT-')!==false 
       || stripos($user_agent, 'Motorola')!==false 
     ) 
     ) return false; 
    } 

    return true; 
} 

或者更好的東西,哈哈:)

+2

你會更好使用更強大的東西:) – neebz 2011-04-30 22:22:49

+0

@nEEbz,當然。提供你的代碼,我會用。 – 2011-04-30 22:24:38

0

您可以查詢字符串參數到你的網站地址添加如?fullsite=true,包括您,如果條件以下>

var fullsite = getQueryString()["fullsite"]; 
if (fullsite != "true" && (screen.height <= xyz || screen.width <= abc)) //now redirect 

您將需要以下函數訪問查詢字符串。我把它從這裏>JavaScript query string

function getQueryString() { 
    var result = {}, queryString = location.search.substring(1), 
     re = /([^&=]+)=([^&]*)/g, m; 

    while (m = re.exec(queryString)) { 
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); 
    } 

    return result; 
} 

而且你可以有鏈接>

<a href="mysite.com?fullsite=true"> Show me Full Site </a>

===========

話說,請花看CSS媒體查詢。它可能需要改變一點你的設計架構,但它非常有用。

+0

我試圖通過寬度/高度檢測移動瀏覽器並重定向的原因(我主要關注的是iPhone和機器人類型電話btw),這是因爲從我的理解用戶代理不是100%可靠的,總是必須更新。然後JavaScript也沒有,因爲很多較低端/較小的設備不會選擇em等等。然後,這些「無所事事」,讓瀏覽器通過媒體類型來完成工作,但一些高端手機忽略了這一點。它使我很難,因爲這不是我的強項。我的?你們能不能指點我一個好的教程?再次感謝大家 – somdow 2011-05-01 00:26:46

8

使用PHP通過$_SERVER['HTTP_USER_AGENT']檢測移動用戶。 JavaScript檢測可能不可靠,因爲許多移動瀏覽器不支持JS。 「查看完整網站」會設置一個Cookie來拒絕移動網站,這是可檢測的。 使用cookie來跟蹤用戶的偏好。

在骨架

<?php 

if (isset($_COOKIE['nomobile'])) { 
    $style = "normal"; 
} else { 

if (preg_match('/iPhone|(...etc...)/', $_SERVER['HTTP_USER_AGENT'])) { 
    $style = "mobile"; 
} else { 
    $style = "normal"; 
} 

} 

對於 「查看完整的站點」 頁面:

<a href="fullsite.php">Full Site</a> 

fullsite.php

<?php 
    setcookie('nomobile', 'true'); 
    header('Location: index.php'); 
?> 
0

服務器端檢測,絕對是做到這一點的方式,如您無法保證JS可用或甚至打開。一個偉大的PHP腳本移動檢測在這裏找到http://detectmobilebrowsers.mobi/它在網絡上得到了很多的使用。

2

首先,到以下網址並下載mobile_detect.php文件:

http://code.google.com/p/php-mobile-detect/

接下來,按照頁面上的說明,並上傳mobile_detect.php到根目錄, 插入在您的索引或主頁的下面的代碼:

<?php 
    @include("Mobile_Detect.php"); 
    $detect = new Mobile_Detect(); 
    if ($detect->isMobile() && isset($_COOKIE['mobile'])) 
    { 
    $detect = "false"; 
    } 
    elseif ($detect->isMobile()) 
    { 
    header("Location:http://www.yourmobiledirectory.com"); 
    } 
    ?> 

你會發現,上面的代碼檢查一個名爲「移動」的cookie,當移動設備被重定向到這個cookie設置移動頁面。設置cookie插入您的移動着陸頁的下面的代碼:

<?php 
    setcookie("mobile","m", time()+3600, "/"); 
    ?> 

查看在爲文章全文:http://www.squidoo.com/php-mobile-redirect