【问题标题】:Redirect based on screen resolution with jQuery?使用jQuery基于屏幕分辨率重定向?
【发布时间】:2010-11-23 23:59:22
【问题描述】:

是否可以使用 JQuery 根据用户的屏幕分辨率重定向到不同的 index.html?例如,宽度为 1024px 的屏幕会转到 1024.html,而其他所有屏幕都会转到正常的 index.html?

我最好喜欢为此使用 jQuery。任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: jquery html css xhtml


    【解决方案1】:

    你不需要 jQuery。

    你可以使用screen.widthwhich works in all browsers

    if (screen.width <= 1024) window.location.replace("http://www.example.com/1024.html")
    else window.location.replace("http://www.example.com/index.html")
    

    http://www.dynamicdrive.com/dynamicindex9/info3.htm

    【讨论】:

      【解决方案2】:
       if ($(window).width() <= 1024) location.href = "1024.html";
      

      docs on width()

      【讨论】:

        【解决方案3】:
        $(document).ready(function() {
            if (screen.width >= 1024) {
                window.location.replace("http://example.com/1024.html");
            }
            else  {
                window.location.replace("http://example.com/index.html");
            }
        });
        

        answer to this question 中关于window.location.replacewindow.location.href 之间区别的参考说明。

        【讨论】:

        • 关于 location.replace 的观点很好,但为什么要等到 document.ready?
        • document.ready 没有特别的原因,只是对 jQuery 问题的自然响应。
        【解决方案4】:

        使用上述答案中的else 语句如果用于 index.html(即用户默认登陆的页面)会导致无限循环

        对于简单的桌面页面到移动页面重定向

        在您的 index.html 的 &lt;head&gt; 中:

        <script>
            if (screen.width <= 1024) {
            window.location.replace("http://www.yourMobilePage.html");
        }
        </script>
        

        【讨论】:

          【解决方案5】:

          Ben,它似乎有效,但有 2 个问题: 如何保持用户所在的实际页面?

          如何自动加载脚本?调整大小时,必须刷新页面才能重定向。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-25
            • 2012-07-10
            • 1970-01-01
            • 1970-01-01
            • 2012-12-10
            相关资源
            最近更新 更多