1

I'm using jquery mobile and I'm in dealing with a problem:

I have 2 html pages: index.html and page.html

//index.html
...
<script src="script.js"></script>
<body>
<div id='contents'></div>
</body>
...

//script.js
$(document).ready(function(){
   $.ajax({
      ...
       success:
       $("#contents").html(content_to_display).trigger('create');
       ...
   });
});

and another page that calls the previous

//page.html

...
<body>
<a href="index.html" data-icon="arrow-l" data-role="button">Back</a>
</body>
...

the problem is that when I click on the Back button (after refreshing the page.html), the page index.html is displayed correctly, except the that stays empty unless I manually refresh the page (F5 or Ctrl+R) displaying the content I want to be displayed.

how to load the index.html page without manually refreshing the page every time?

Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68

3 Answers3

1

Refresh is your problem, this is a timeline of what have happened:

  1. index.html is loaded
  2. page is changed to page.html and its body content is loaded into index.html DOM but link is still domain/page.html
  3. Because of full page refresh, page.html is reloaded without a HEAD content of index.html.
  4. page is changed to index.html and its body content is loaded into page.html DOM WITHOUT its HEAD part and that is a reason why javascript is not triggered

This problem will be fixed if both index.html and page.html have identical HEAD content, mainly reference to the same custom js file. For more solution to this problem (with solution) take a look at this ARTICLE, to be more transparent it is my personal blog. Or it can be found HERE.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
0

I think you are looking for something like:

 window.onbeforeunload = function () {
     // stuff do do before the window is unloaded here.
 }

this runs before the user is navigated away from the current page.

this means you can force the user to navigate to your page which will refresh correctly.

SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
  • it doesn't work. I edited my previous question. The issue appears only if I refresh the page.html page and after I click on Back. If I don't refresh the page.html before the click, the index.html page is displayed correctly... – Dario Rusignuolo Feb 06 '13 at 16:17
  • ah, maybe I understand what you say... you are saying to move the js include on the page.html instead of index.html? so that the jquery loads the contents of the index.html before it lefts the page.html page? – Dario Rusignuolo Feb 06 '13 at 17:01
0

Try this instead of the backbutton if you are just navigating back one page:

<a href="javascript:history.go(-1)">BACK</a>

Or give this a shot:

$.mobile.changePage(href, {changeHash: false });
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82