網站開發中web頁面跳轉幾種方式詳解

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   java   

在做web開發中,頁面跳轉的方式有很多種,然而有些時候這些跳轉如何用到恰到好處卻很容易被忽視。

用戶端觸發跳轉有如下幾種
  1. 使用meta元資訊

<!--如下表示5秒後跳轉到url指定的連結,推薦使用這種方式--><meta http-equiv="refresh" content="5;url=http://my.oschina.net/ososchina/blog">

  2.使用javascript中的window.location對象

<!--這裡的代碼忽略了搜尋引擎對於連結價值的轉移,它會被當做‘暫時地‘重新導向。--><script type="text/javascript">//修改window.location.hrefwindow.location.href =‘http://my.oschina.net/ososchina/blog‘;//替換url,注意這種方式有時並不是有效,需要強制reload才行//window.location.replace(‘http://my.oschina.net/ososchina/blog‘);//window.location.reload(true);//修改window導航//window.navigate (‘http://my.oschina.net/ososchina/blog‘) </script>;

補充一點:在html5中,利用新的api實現前端頁面mvc的跳轉也是一種不錯的方式(注意:服務端不跳轉),利用這種方式可以實現前端MVC開發

<script type="text/javascript">var state = { action : "page", title : "HTML 5 History API simple demo",url : "yourpage"};history.pushState(state, "HTML 5 History API simple demo", state.url);window.onpopstate = function (e) {switch (e.state.action) { case "home" :    document.title = "HOME ……";    $.get("index.php").done(function (data) { $("#wrapper").html(data); });     break;   case "page" :     document.title = e.state.title;     $.get(e.state.url).done(function (data) { $("#wrapper").html(data); });   break; }}</script>;

以上是用戶端觸發的跳轉

-------------------------------------------------------------------------------------------

服務端觸發的跳轉的方式
  1. php中使用header跳轉

header(‘Location:http://my.oschina.net/ososchina/blog‘); //這裡的代碼忽略了搜尋引擎對於連結價值的轉移,它會被當做‘暫時地‘重新導向。

推薦使用下面的方式

header(‘HTTP/1.1 301 Moved Permanently‘);//固定重新導向header(‘Location: http://my.oschina.net/ososchina/blog‘);

有時,設定http 回應標頭是非常重要的,這些可以協助搜尋引擎和瀏覽器“理解”請求的狀態

下面提供一種發送http回應標頭的php函數

/** * 發送HTTP狀態 * @param integer $code 狀態代碼 * @return void */function send_http_status($code) {    static $_status = array(            // Informational 1xx            100 => ‘Continue‘,            101 => ‘Switching Protocols‘,            // Success 2xx            200 => ‘OK‘,            201 => ‘Created‘,            202 => ‘Accepted‘,            203 => ‘Non-Authoritative Information‘,            204 => ‘No Content‘,            205 => ‘Reset Content‘,            206 => ‘Partial Content‘,            // Redirection 3xx            300 => ‘Multiple Choices‘,            301 => ‘Moved Permanently‘,            302 => ‘Moved Temporarily ‘,  // 1.1            303 => ‘See Other‘,            304 => ‘Not Modified‘,            305 => ‘Use Proxy‘,            // 306 is deprecated but reserved            307 => ‘Temporary Redirect‘,            // Client Error 4xx            400 => ‘Bad Request‘,            401 => ‘Unauthorized‘,            402 => ‘Payment Required‘,            403 => ‘Forbidden‘,            404 => ‘Not Found‘,            405 => ‘Method Not Allowed‘,            406 => ‘Not Acceptable‘,            407 => ‘Proxy Authentication Required‘,            408 => ‘Request Timeout‘,            409 => ‘Conflict‘,            410 => ‘Gone‘,            411 => ‘Length Required‘,            412 => ‘Precondition Failed‘,            413 => ‘Request Entity Too Large‘,            414 => ‘Request-URI Too Long‘,            415 => ‘Unsupported Media Type‘,            416 => ‘Requested Range Not Satisfiable‘,            417 => ‘Expectation Failed‘,            // Server Error 5xx            500 => ‘Internal Server Error‘,            501 => ‘Not Implemented‘,            502 => ‘Bad Gateway‘,            503 => ‘Service Unavailable‘,            504 => ‘Gateway Timeout‘,            505 => ‘HTTP Version Not Supported‘,            509 => ‘Bandwidth Limit Exceeded‘    );    if(isset($_status[$code])) {        header(‘HTTP/1.1 ‘.$code.‘ ‘.$_status[$code]);        // 確保FastCGI模式下正常        header(‘Status:‘.$code.‘ ‘.$_status[$code]);    }}

然後我們來最佳化一下

send_http_status(‘301‘);//固定重新導向header(‘Location: http://google.com/‘);

問題來了:這裡挖掘機技術必須強,使用http響應後需要注意的是,在回應標頭發送之前必須沒有輸出資料流輸出內容,因此,這種讓人滿意的方法有時並不適用;下面進行改造:

<?php// 如果之前沒有任何輸出, 就送出 Location 的導向資訊if (!headers_sent()) {    send_http_status(‘301‘);//固定重新導向    header(‘Location: http://my.oschina.net/ososchina/blog‘);    exit;}else{  echo "<script type=‘text/javascript‘>";  echo "window.location.href =‘http://my.oschina.net/ososchina/blog‘;"  echo ‘</script>‘}?>

--------------------------------------------------------------------------------

在頁面有2個紅色的詞語“觸發”,這裡的意思是通過觸發事件來進行跳轉的,在http請求中(http基於tcp串連),一般是 “請求-應答”模式,但這種模式是獨立於頁面跳轉的。

實際上頁面跳轉分為2中方式 :用戶端跳轉,用戶端和服務端一起跳轉

用戶端跳轉在目前mvc主流架構中使用較為廣泛,這種跳轉一般通過服務端控制(Controller)觸發,如j2ee servlet中的Dispatcher和forward以及php中的include,display,用戶端和服務端一起跳轉一般具有強制性,這個無論前端和後端均可使用,但後端使用的一個好處是可以發送狀態代碼,對於SEO等工作具有很多好處。


網站開發中web頁面跳轉幾種方式詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.