Javascript 如果页面大小小于700px,asp.net将重定向到另一个url
我有一个页面,例如Javascript 如果页面大小小于700px,asp.net将重定向到另一个url,javascript,c#,jquery,asp.net,Javascript,C#,Jquery,Asp.net,我有一个页面,例如desktop.aspx?customerID=345和mobile.aspx?customerID=345。两个页面具有相同的功能。所以假设用户转到这里desktop.aspx?customerID=345,屏幕大小小于700px,那么它应该重定向到mobile.aspx?customerID=345。现在使用Jquery我可以做这个重定向,但这里还有动态查询字符串 是否可以在.aspx文件的代码隐藏中检测屏幕大小?要检查屏幕和浏览器宽度,可以使用javascript匹配媒体
desktop.aspx?customerID=345
和mobile.aspx?customerID=345
。两个页面具有相同的功能。所以假设用户转到这里desktop.aspx?customerID=345
,屏幕大小小于700px,那么它应该重定向到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>
var hdncustomerID=$('hdncustomerID').Val()//如果您使用Jquery
$(文档).ready(函数(){
如果($(窗口).width()<700){
window.location=mobile.aspx?customerID=123;
}
});
解决方案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());
}
受保护的无效页面加载(对象发送方,事件参数e)
{
var CustomerID=1;
StringBuilder strScript=新建StringBuilder();
strScript.Append(“”);
Append(“$(document).ready(函数(){”);
追加(“if($(window.width()<700){”);
追加(“window.location=mobile.aspx?customerID=”);
strScript.Append(CustomerID);
strScript.Append(“;”);
strScript.Append(“}”);
strScript.Append(“}”);”;
strScript.Append(“”);
ClientScriptManager脚本=Page.ClientScript;
script.RegisterClientScriptBlock(this.GetType(),“redirect”,strScript.ToString());
}
希望它的工作 为什么它需要在代码背后而不是客户端?您可以使用JQuery。如果需要返回服务器,可以使用AJAX将屏幕大小传递回服务器server@Alex请给我看一些虚拟代码,我可以在其中解析jquery值以在代码隐藏文件中字符串。您可以使用用户的分辨率设置cookie并在代码隐藏中使用它,而无需使用AJAX(取决于用例)。但请注意,700px是一个确定电话的任意数字。例如,我的手机的水平像素比这个多(2560x1440)。@JohanB你能给我看一个吗?手机和桌面的两个不同页面?这正是CSS媒体查询在一个页面中解决这两个问题的目的。我需要将大小解析为后面的代码&在那里我需要检查。如果屏幕大小为