I'm trying to have a single page that can show the same type of data (lets say fruits). Then I want to load this page anywhere in my website hierarchy but each time with different parameters.
I have my main page like the following with two links to the same page show.html:
- <div data-role="navbar" data-iconpos="top" data-theme="a" class="nav-ecommera">
- <ul >
- <li>
- <a href="show.html?p=apples" data-role="button">Apples</a>
- </li>
- <li>
- <a href="show.html?p=oranges" data-role="button">Oranges</a>
- </li>
- </ul>
- </div>
A click on each of the two buttons will create a new instance of the page show.html in the DOM. All of the items in show.html therefore will have duplicate ID's in the DOM.
In my javascript I want to dynamically fill the show.html page:
- $('div[id="show"]').live("pagebeforeshow", function(e, data) {
- var p = getUrlParameter("p");
- show(p);
- });
- var show = function(p) {
- $.ajax({
- url:'http://show.com/?p='+p,
- success: function(data) {
- // Refresh 'show' page with new data
- // First time: It's fine.
- // Second time: 'show' page is duplicated in the DOM so it's messy.
- }
- });
- }
Now the first time show.html loads everything is fine. However the second time everything in show.html is loaded twice and the DOM includes many duplicate ID's.
Is there a way to remove the first page from DOM before loading the new one?
Or:
Is there a better approach that will do what I'm trying to achieve here?
Note: I already tried removing previous instances of show pages when loading a new one. It works as far as showing the second page is concerned. But there is a problem when the first page needs to be shown for the second time, after being manually removed.
I think the reason is jQuery mobile seems to think the first page is already loaded, despite the fact that we manually removed it. So it doesn't fully reload the first page when accessed again. I'm talking about this sequence of navigation: Home -> Apples -> Back to home -> Oranges -> Back to home -> Apples (Here you get a defected page).