Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery mobile:加载前重定向到另一个页面_Jquery_Redirect_Jquery Mobile - Fatal编程技术网

Jquery mobile:加载前重定向到另一个页面

Jquery mobile:加载前重定向到另一个页面,jquery,redirect,jquery-mobile,Jquery,Redirect,Jquery Mobile,am使用jquery mobile 1.0 我想从first.html重定向到otherPage.html,而不显示first.html -我几乎尝试了所有的活动,但没有成功。此时会显示first.html 然后,我尝试了“pagebeforeload”事件,但它没有被触发 $( document ).bind( "pagebeforeload", function( e, data ){ console.log("pagebeforeload starting"); //

am使用jquery mobile 1.0

我想从first.html重定向到otherPage.html,而不显示first.html

-我几乎尝试了所有的活动,但没有成功。此时会显示first.html

然后,我尝试了“pagebeforeload”事件,但它没有被触发

$( document ).bind( "pagebeforeload", function( e, data ){ 

        console.log("pagebeforeload starting"); // NO LOGGING HAPPENING 
        window.location.href = "otherPage.html";

        e.preventDefault(); 

        data.deferred.resolve( data.absUrl, data.options, response.page); 

}); 

$( document ).bind( "pageload", function( e, data ){
    console.log("Page successfully loaded into DOM..."); // NO LOGGING HAPPENING 
});
-我不太清楚这件事&没有找到一个可行的例子。
-有人能帮忙吗

pagebeforeload事件仅在加载外部页面时触发:

每当外部页面加载到应用程序DOM中时,都会触发2个事件。第一个是pagebeforeload。第二个事件将是pageload或pageloadfailed

我唯一的解决办法是异步加载first.html的页面内容,这并不理想,因为最终会对同一页面发出两个http请求。但是,这将使您能够仅在需要时调用first contents.html,否则您可以重定向到otherPage.html,而不显示任何内容。例如:

<script>
    someCondition=true;
    if(someCondition) {
        window.location.href = "otherPage.html"; 
    } else {
        $(document).ready(function() {
            $('body').load('first-content.html', function() {
                //other stuff after the contents have loaded: like the rest of your jquery relating to first.html
            });
        });
    }
</script>

someCondition=true;
如果(某些条件){
window.location.href=“otherPage.html”;
}否则{
$(文档).ready(函数(){
$('body').load('first-content.html',function(){
//加载内容后的其他内容:比如与first.html相关的jquery的其余部分
});
});
}

确保first.html页面中的body标记为空。

您是否尝试过不使用事件。JS代码将在遇到它时执行,因此确保您的条件代码位于页面顶部(在头部某处)就足够了

<html>
<head>
<script>
  if (condition)
  {
    window.location.href = "otherPage.html";
  }
</script>
<!-- rest of page -->

如果(条件)
{
window.location.href=“otherPage.html”;
}

很难说出你想要完成什么。与内噬菌体状态类似,如果您只想转到另一个页面,则可以执行简单的JavaScript重定向

我假设您希望能够选择是显示第一页还是第二页的内容,但仍然保持在第一页

您可以添加以下代码。确保在包含jquery.mobile之前添加它

<script>
    $(document).bind('mobileinit', function () {
        // disable autoInitialize
        $.mobile.autoInitializePage = false;
    });
    $(function () {
        var displaySecondPage = true;
        if (displaySecondPage) {
            // load page2.html and replace #page1 with contents of #page2 on page2.html
            $('#page1').replaceWith($('<div />').load('page2.html #page2', function() {
                // continue initialize
                $.mobile.initializePage();
            }));
        }
        else {
            // continue initialize
            $.mobile.initializePage();                
        }
    });
</script>
jquerymobile负责显示和隐藏页面,因此默认情况下将它们全部隐藏将确保您不会看到未增强的内容

无论如何,希望这有帮助。

尝试使用以下方法:

<head> 
    <script>
        window.location.replace('otherPage.html');
    </script> 
</head>

window.location.replace('otherPage.html');

试试这个

$("#firstPageId").live("pagecreate",function(){
         $.mobile.changePage("otherPage.html");
});
此外,您还可以使用loadPage


希望能有所帮助:)

我知道这很旧,但我花了一些时间试图解决这个问题,所以这可能对某些人有用

现在,使用jquerymobile1.3,您只需将数据url设置为目标页面上的
。JQM将检测到这一点并相应地更新URL

在这里看到更多


这里甚至有一些演示

,因为似乎有很多答案;尤其是在使用多页模板时。。这里有一个链接,指向jquery移动表单上的一个答案,它通过在页面初始化之前将#标记注入url来工作


在我的情况下不起作用,因为我不希望用户查看哈希标记,但这只是一个开始。。以后还会有更多内容

我只能对自己的答案发表评论;但在发布我的答案之前,我确实尝试了@Endophage的答案,但出于某种原因,在移动设备上重定向之前,它仍然会显示页面,尤其是在首次加载(即没有缓存)和较大页面大小时。Thnx用于回复。唯一我想要的是-一个事件,在该事件中重定向将触发&进一步的页面加载将停止。否则,只需显示/隐藏page.thnx进行回复就更容易了。但它不起作用,此页面在重定向前闪烁:(@AvisekChakraborty好吧,这大概是你对JS的最好期望了,所以如果它不起作用,你有两个选择。要么接受它足够好,要么解决如何重定向服务器端…你找到答案了吗..我需要解决方案..提前谢谢..嗨,事实上我差不多一年前就这么做了。但是我会搜索并让你知道的。谢谢你ou..如果您找到了,请告诉我..谢谢,我正在从previosPage.html重定向到first.html或otherPage.html。因此重定向逻辑现在在previosPage.html中
<head> 
    <script>
        window.location.replace('otherPage.html');
    </script> 
</head>
$("#firstPageId").live("pagecreate",function(){
         $.mobile.changePage("otherPage.html");
});