window.location not work properly

久未见 提交于 2019-12-21 06:37:49

问题


Hi all I am navigating from one html page to another as:

window.location = "test.html";  

In test.html I have header as:

<script type="application/javascript">

            function redirect()
            {
                alert("inside redirect:");
                //window.location = url;
                //history.back();
                history.go(-1);
                return false;

            }

        </script>

        <div data-role="navbar">
            <ul>

                <li> <a href="#" data-theme="b" onclick="history.back(); return false;">Go Back</a></li>
                <li> <a onclick="redirect()" data-icon="back" data-iconpos="notext" data-direction="reverse" class="ui-btn-left jqm-home">Back1</a></li>

            </ul>
        </div>

But here both back buttons (Go Back and Back1) are not working. If I use $.mobile.changePage('test.html') to navigate between pages then both Back buttons work. Why they are not with windows.location?

I want to develop this application for B-Grade browser which are not support ajax. Therefore I cant use $.mobile.changePage('test.html').

Any help will be appreciated. Thanks.


回答1:


This Blackberry OS 5 browser has lots of issue. Initially i also tried to do what you are doing right now. But later i think so you will have to think of some other approach which is better. Here is how i solved it

First of all add these lines before loading the jquery-mobile script like this

<script type="text/javascript">
    $(document).bind("mobileinit", function() {
        $.support.cors = true;
        $.mobile.allowCrossDomainPages = true;
    });
</script>

Then i used the Single Html concept. In this concept i had to load my script just once and i could make use of jquery-mobile's changePage. When i had many html pages then i has to wait for some seconds as loading and unloading of scripts took place. Avoid that, Its unnecessarry.

Have all the pages with in the same Html like this

<div data-role="page" id="page1">
<div data-role="page" id="page2">
.
.
.

Then after that you can easily do a changePage like this

$.mobile.changePage("#page1", {
    transition : "slide"
});

I hope you take the right approach.




回答2:


Why not use window.location.replace("test.html");




回答3:


try:

window.location.href = "test.html"; 

OR:

window.location.assign("test.html");

SEE REFERENCE




回答4:


In jQuery mobile you can turn of ajax by setting some initiation variables. You need to load this script before you load jQuery mobile

$(document).bind("mobileinit", function(){
  //apply overrides here
  $.mobile.ajaxEnabled = false;
});

With ajax turned off you can now still use your $.mobile.changePage().

Now to create a back button all you have to do is give the link an attribute of data-rel="back"

<li><a href="#" data-theme="b" data-rel="back">Go Back</a></li>


来源:https://stackoverflow.com/questions/11663279/window-location-not-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!