簡體   English   中英

如果頁面大小小於700px,asp.net重定向到另一個URL

[英]asp.net redirect to another url if page size is less then 700px

我有一個頁面,例如desktop.aspx?customerID=345mobile.aspx?customerID=345 兩個頁面具有相同的功能。 因此,訂閱者用戶轉到此處desktop.aspx?customerID=345並且屏幕尺寸小於700像素,則應重定向到mobile.aspx?customerID=345 現在,使用Jquery我可以執行此重定向,但是在這里,我也具有動態查詢字符串。

是否可以在.aspx文件后面的代碼中檢測屏幕大小?

要檢查屏幕和瀏覽器的寬度,可以使用javascript匹配媒體:

對於瀏覽器:

var isBrowserLessThan700 = (window).matchMedia('screen and (max-width: 700px)').matches;

對於屏幕:

var isDeviceLessThan700 = (window).matchMedia('screen and (max-device-width: 700px)').matches;

如果結果為真:

   window.loacation.href= "What-You-Want";

解決方案1:在客戶端中創建一個隱藏字段變量。

.ASPX文件:-

<asp:HiddenField ID="hdncustomerID" runat="server" />

腳本:-

<script type="text/javascript">
 var hdncustomerID = $('hdncustomerID').Val(); //if u use Jquery

        $(document).ready(function () {
            if ($(window).width() < 700) {
                window.location = mobile.aspx?customerID=123;
            }
        });
</script>

解決方案2:-

protected void Page_Load(object sender, EventArgs e)
    {
        var CustomerID = 1;

        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script type=\"text/javascript\">");
        strScript.Append("$(document).ready(function () {");
        strScript.Append("if ($(window).width() < 700) {");
        strScript.Append("window.location = mobile.aspx?customerID='");
        strScript.Append(CustomerID);
        strScript.Append("';");
        strScript.Append("}");
        strScript.Append(" });");
        strScript.Append("</script>");

        ClientScriptManager script = Page.ClientScript;
        script.RegisterClientScriptBlock(this.GetType(), "redirect", strScript.ToString());

    }

希望它的工作!

暫無
暫無

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

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