簡體   English   中英

使用jquery / ajax在加載頁面之前檢查頁面的可用性

[英]Check Availability of a page before loading it, using jquery/ajax

加載之前是否可以檢查頁面的可訪問性?

我有一個表格,使用無線連接在移動設備上運行。 問題是:並非總是可以使用此連接,我想在提交或卸載頁面時提醒用戶。

問題在於頁面包含執行重定向的元素,如下所示:

<input type="button" value="MyText" onClick="script1;script2;...window.location='mylocation'" />

如果用戶單擊此按鈕,並且服務器無法實現,我將收到一些令人討厭的錯誤。

另外,如果我想概括我的腳本,我以前也不知道“ mylocation”的值。

該頁面還包含提交表單的元素:

<input type="submit" name="SUBMIT" value="MyValue" onClick="return eval('validationForm()')" />

對於提交,我使用的是ajaxForm插件,並且效果很好。

要輕松導航,請改用以下代碼:

<input type="button" value="Back" onClick="window.location='history.go(-1);" >

其中-1表示上一頁。 如果要重新加載當前頁面,請改用0,向前瀏覽,請使用1,依此類推。

如果您使用jquery中的ajax,它會自行處理... http://api.jquery.com/jQuery.ajax/

$.ajax({
        ///... need argument here...

        timeout: 5000, // in milliseconds
        success: function(data) {
             //Do something success
        },
        error: function(request, status, err) {
            if(status == "timeout") {
               alert("can't reach the server");
            }
        }
    });

編輯后的評論:您可以檢查如何檢查jQuery或JavaScript中是否存在文件? 在您的情況下,這項艱巨的工作符合預期:

//Initialize the var as global so you can use it in the function below
var goto_url = "http://www.mywebsites.com/foo.html"; 

$.ajax({
    url:goto_url;
    type:'HEAD',
    error: function()
    {
        //do something if the gile is not found
    },
    success: function()
    {
         document.location = goto_url; //docuemnt.location will redirect the user to the specified value.
    }
});

這實際上將檢查文件是否存在。如果無法連接到文件,則將無法找到。如果他可以找到文件,則他顯然可以連接,所以無論哪種情況,您都可以贏。

干杯!

您應該使用jQuery Ajax函數的回調來解決服務器不可用的問題。

您無法在未請求的情況下檢查服務器的可用性。

感謝您的回答,我找到了解決問題的方法。 在啟動腳本和重定向之前,請檢查服務器是否可訪問。 那是代碼:

 function checkConnection(u,s){
    $.ajax({
        url:u,
        cache:false,
        timeout:3000,
        error: function(jqXHR, textStatus)
        {
            alert("Request failed: " + textStatus ); 
        },
        success: function()
        {
            eval(s);   
        }
    });       
 }      

    $(document).ready(function() {
        // part of the function that checks buttons with redirect
        // for any button that contain a redirect on onClick attribute ("window.locarion=")
        $("input[type=button]").each(function(){
            var script = $(this).attr("onClick"); 
            var url = "my_url";
            var position = script.indexOf("window.location") ;  
            if (position >= 0) {       // case of redirect           
               url = "\'"+url+"\'"; // that's my url
               script = "\""+script+"\""; // that's the complete script
               $(this).attr("onClick","checkConnection("+url+","+script+")");
            }
        });
        // part of the function that checks the submit buttons (using ajaxForm plugin)
        var options = {
                    error: function() {                    
                        alert("Message Error");   
                    },
                    target: window.document,
                    replaceTarget: false,
                    timeout:   3000
                };
                $("form").ajaxForm(options);
    });

我希望這會有用。

您正在嘗試查看是否存在連接。 AFAIK實際檢查服務器是否可訪問的唯一方法是向該服務器發出請求。

將超時時間設置為相當短的時間(例如3秒),然后提出請求。 如果收到超時錯誤,則說明沒有連接,否則可以發送表格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM