Search code examples
javascriptjqueryjquery-mobilebrowser-historyhashchange

javascript & jQuery mobile: navigate to <div data-role="page"></div>


I am relatively new to jQuery Mobile. So, I do not know every corner of the framework capabilities.

I am currently facing some problems with history and dynamic page navigation.

Basically, I want to achieve the following:

I have a web page that changes content by button clicks. The "home" page should display the <div data-role="page" id="list_root">...</div>. By clicking a button the page should navigate to <div data-role="page" id="list_123">...</div>. I want to be able to get back to the content of div "#list_root" by using the back button.

Prior to using jQuery I did implement this by myself with history.pushState(divContent, null, "#list_<id>") and replacing the old content with the new one. However, the pushState plugin is deactivated by default using jQuery mobile.

The button that is pressed to navigate is defined like the following:

<a href='#list_<id>' class='...'>...</a> 

I did try to implement the creation of the new div content (for list_) by implementing a callback for "onhashchange". But this event gets never called.

Futhermore, if I would be able to create the content when the link was clicked, I would like to show the content of <div id="list_<id>">, replacing the previous content.

Since the application presents mass data - I did not want to use jQuery mobiles' nested list.

Do you have any hints for me how I can implement this using the framework functionality?

EDIT:

I have the following situation:

<div data-role="page" class="type-interior" id="main_window">
    <div id="current" class="ui-content" data-role="content" role="main">
        <div id="list_root" data-url="list_root">
        <div id="list_123" data-url="list_123" data-role="page">
    </div>
</div>

As you see "list_root" is the default "start page" and gets displayed as intended.

I added an event listener for click() to dynamically create the content of <div id="list_123"> via:

document.addEventListener('click', function(e) {
       ... create it ...
       e.stopPropagation();
   }, true)

This works.

The button is created like the following:

<a href="#list_123" data-role="button">List_123</a>

The div content gets created successfully and after that the hash in the url changes to #list_123. But the page is not displayed. Is the problem here, that <div data-role="page" id="list_123">...</div> stands within the <div data-role="page" class="type-interior" id="main_window"> tag? I'll try that next!


Solution

  • as stated above - the problem that I had was, that a new page to which you want to navigate has always to be inside <div data-role='page'><div data-role='content'>....</div></div> tags and a child element of the body tag.

    Now it works fine!

    Thanks!