Getting a function to run everytime a jquery mobile page is loaded - jquery-mobile

I'm trying to get a function to run everytime jquery mobile loads a new page into the application, but currently, the code only gets run once on the first instance of a page loading.
HTML:
<div id="pageID" data-role="page" class="content_page">
<div data-role="header" data-id="bestHeader" data-position="fixed">
<h1>Header</h1>
</div><!-- end header -->
<section data-role="content">
<h2>Header2</h2>
<p>Content goes here</p>
</section>
<div class="bottomAd" data-role="footer" data-position="fixed">
<img src="ad2.jpg" width="100%">
</div>
<div data-role="footer" data-id="bestFooter" data-position="fixed">
<nav data-role="navbar">
<!-- navBar here -->
</nav>
</div><!-- end footer -->
</div><!-- end page -->
<div id="pageID2" data-role="page" class="content_page">
<div data-role="header" data-id="bestHeader" data-position="fixed">
<h1>Header</h1>
</div><!-- end header -->
<section data-role="content">
<h2>Header2</h2>
<p>Content goes here</p>
</section>
<div class="bottomAd" data-role="footer" data-position="fixed">
<img src="ad2.jpg" width="100%">
</div>
<div data-role="footer" data-id="bestFooter" data-position="fixed">
<nav data-role="navbar">
<!-- navBar here -->
</nav>
</div><!-- end footer -->
</div><!-- end page -->
Javascript
$(".content_page").live('pageinit', function(event) {
if ($(".bottomAd img[src*=ad]").length >= 1) {
console.log($(".bottomAd img[src*=ad]").length);
$(".bottomAd").remove();
}
No matter how many times you click back and forth between the two pages, the console.log only gets called once, and the .bottomAd class gets removed document wide on the first instance of the page getting clicked on. I'd like to have the function run everytime the page is called, so each time the "page" is checked for having an image with a src that contains "ad" in the bottomAd div on that specific page, and then removed if so.

If you want to do work on something each time a page is shown (either the first time or subsequent visits) then I suggest using pageshow, pageinit only fires the first time a page is shown and jQuery Mobile will keep pages in the DOM sometimes. If you want to run some code only the first time a user goes to a page then use pageinit but bind your delegated event handler to the document element.
I would also make your selector more specific (to the current page) so you don't work on elements on pages not being viewed like so:
...
//`this` refers to the `<data-role="page">` pseudo-page element being bound to
$(this).find(".bottomAd").remove();
..
Your delegated event handler should bind to an element always in the DOM, for example document:
$(".content_page").live('pageinit', function(event) {
should be
$(document).on('pageinit', '.content_page', function () {...});
Note that .live() was depreciated in jQuery 1.7 and that .on() is the new flavor.

You probably want to use the pageshow event, you also might want to use .on instead of .live as .live is depreciated,
For example
$(document).on('pageshow','.content_page' function(event) {
if ($(".bottomAd img[src*=ad]").length >= 1) {
console.log($(".bottomAd img[src*=ad]").length);
$(".bottomAd").remove();
}
There is also a pageload event but that is fired when an external page is attached to the DOM.

pageload Event
"Triggered after the page is successfully loaded and inserted into the DOM. Callbacks bound to this event will be passed a data object as its 2nd arg. "
http://jquerymobile.com/test/docs/api/events.html

Related

Jquery Mobile - $(document).ready(function() {});

I am new to jquery mobile.
I need help understanding the equivalent of document.ready() for jquery mobile.
I have 2 jsp pages as follow:
page1.jsp
<head>
<script type="text/javascript">
$(document).on('pagebeforeshow', '#page1', function(event){
alert("page 1");
});
</script>
</head>
<body>
<div data-role="page" id="page1">
<header data-role="header">
</header>
<article data-role="content">
page 1
</article>
<footer data-role="footer" data-position="fixed">
<nav data-role="navbar">
<ul>
<li>Page 2</li>
</ul>
</div>
</body>
page2.jsp
<head>
<script type="text/javascript">
$(document).on('pagebeforeshow', '#page2', function(event){
alert("page 2");
});
</script>
</head>
<body>
<div data-role="page" id="page2">
<header data-role="header">
</header>
<article data-role="content">
page 2
</article>
<footer data-role="footer" data-position="fixed">
<nav data-role="navbar">
<ul>
<li>Page 1</li>
</ul>
</div>
</body>
So when I initially load page 1, I will get an alert "page 1". But when I navigate to page 2, I do not get the alert "page 2"
But on page 2, I navigate back to page 1, I get an alert "page 1"
So why don't I get "page 2" alert when I load page 2 via navigation?
I am trying to learn jquery mobile so please an explanation and any code example would help to me understand it a lot.
I tried to read through jquery mobile documentation but not understanding it.
Thank you!
Your script isn't running because it technically isn't being loaded. jQuery Mobile follows links by intercepting their normal functionality (which is to navigate to a new page) and instead loads the page's content via an AJAX request. If successful, it then takes the [data-role=page] element and adds it to your existing page. The result is that the new page is loaded, but you never really leave the page that you started from.
Anything not within a [data-type=page] element is not loaded during this, including your scripts. You can fix this by by moving your <script> tag inside of page2's [data-role=page] element. If you do this, keep in mind that any code loaded this will will remain as other pages are loaded, and you might have things start interacting in ways you never intended.
Another option is to simply force jQuery Mobile to actually navigate to new pages when links are clicked. Check out their documentation for how to do that.

Jquery mobile dialog close going to far back

I currently have a dialog that allows you to download a file, and another link (the one in question) to transfer the user to another page which links you to an external API.
When I pop up the dialog, I can click on close and the dialog closes properly. When I follow the external link the page loads correctly and everything like that.
However if I hit the back button, it keeps the #ui-dialog has in the URL and nothing is loaded into the page.
I have tried to set the changehash value to false, but when I do that and if I close the dialog, it takes me too far back in my browsers history.
$(document).ready(function () {
VaultManager.GetFolderConent(VaultManager.UserRootFolderGuid, "My Vault");
$.mobile.changePage.defaults.changeHash = false;
});
function PopDialog() {
$.mobile.changePage( 'MobileFileOptions.htm', { reverse:false, transition:'pop', role:'dialog' });
}
Instead of PopDialog(), I also tried this:
<a href="MobileFileOptions.htm" data-rel="dialog" data-transition="pop">
Here is MobileFileOptions.htm:
<div data-role="page">//I have tried data-role="dialog" as well
<div data-role="header" data-theme="e">
<h1 id="popupHeader">File</h1>
</div><!-- /header -->
<div data-role="content">
<div id="divFileName"></div>
<div id="divFileSize"></div>
<div id="divFileCreated"></div>
<ul data-role="listview" data-inset="true">
<li>Download</li>
<li>
<a href="BoxManager.aspx" data-rel="external" id="boxLink">
Send to Box.NET
</a>
</li>
</ul>
</div><!-- /content -->
</div>
Am I missing something somewhere?
I am fairly new with jQuery Mobile, so, I may have done some mistakes and I can't figure out to get much help from the docs.

Inconsistent page thme with Jquery mobile nested list

In jquery mobile I have a nested list.
When the nested list apears the page is not the same as the previous page.
The page showing the list "1.html" is:
data-theme="a", with a nice header and footer.
The nested list looks like data-theme="d", no footer and has a back button - i would prefer a custom button?
Is there anyway to fixed this so the nested list page is consistent with its fathers' page?
Thanks :-)
A bug? What version of jQuery Mobile are you using? As of version 1.1.0 the back button in the header is disabled by default. Check out this example http://jquerymobile.com/demos/1.1.0/docs/lists/lists-nested.html
About your second question regarding the buttons, the footer and the themes.
To render a button, all you need to do is to put data-role="button" and for a theme add data-theme="a" to a link like this
Link button
To create the footer you need to place the following inside the page div:
<div data-role="footer">
<h4>Footer content</h4>
</div><!-- /footer -->
As a demo, here is a page with the theme e applied:
<div data-role="page" data-theme="e">
<div data-role="header" data-theme="e">
<h1>Single page</h1>
</div><!-- /header -->
<div data-role="content">
<p>Some text here</p>
Link button
</div><!-- /content -->
<div data-role="footer" data-theme="e">
<h4>Footer content</h4>
</div><!-- /footer -->
</div><!-- /page -->
As a tip, you can look the examples here http://jquerymobile.com/demos/1.1.0/ and study the code by selecting "View Page Source" in your browser. This is the way I am learning JQM now.

Header overlaps content DIV in jquery-mobile

i thought this would be a problem of jquery-mobile 1.1.0 RC.
But also in the final release i got this weird problem, that my header overlaps my content div:
When i now click somewhere in the content div, the page rerenders and the content div is at the correct position(directly beneath the header).
I'm using Backbone.js Views for representing content, and jquery-mobile-router. I tried almost everything but i counldn't find any solution for this problem.
Does anyone know a solution for that?
html:
<!-- newsoffers page -->
<div data-role="page" id="newsoffers">
<div data-role="header" data-theme="a" data-fullscreen="false" data-position="fixed">
</div>
<!-- /content -->
<div data-role="content" data-scroll="true" class="content-full">
</div>
<!-- /content -->
<div id="newsoffersFooter" data-role="footer" data-position="fixed" data-fullscreen="false" data-id="mainFooter" data-theme="b">
</div>
<!-- /footer -->
</div>
the code for the header is dynamically injected! The content will be injected while the "pagebeforeshow" event is fired.
"setNavBarCollection" adds a backbone collection to the header, which is then rendered as a navbar.
The "refreshOffers" function generates a view containing a backbone collection which is fetched asynchronously.

get header and footer out of the page dom

I'm building a mobile app with Cordova, Backbone for the MVC structure and jQuery Mobile for UI.
This works well except for the user experience while loading a new page. Actually a new page is created dynamically, and when it's ready, jquery mobile handles the page transition to this page. In a mobile browser this transition is slower than in a classic browser, so while page is loading the user sees a blank white screen and then appears the new page.
The matter is about my header/footer bar. I'd like it to keep showing while the content is loading, so the user will see the header/footer and only a blank/white content during the transition.
So this is a classic page structure:
<div data-role="page">
<div data-role="header"></div>
<div data-role="content"></div>
<div data-role="footer"></div>
</div>
For me the easiest way to go is like that:
<div data-role="header"></div>
<div data-role="page">
<div data-role="content"></div>
</div>
<div data-role="footer"></div>
And fix the content with CSS positioning.
But I'd like to find something smarter.
What do you think of having a single JQM page, with Backbone just updating the <div data-role="content"> for each route? What about JQM rendering? And what about transitions?
Documentation: http://jquerymobile.com/demos/1.1.0/docs/toolbars/footer-persist-d.html
Basically you put a data-role="footer" element in each page like this:
<div data-role="footer" data-id="foo-footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Friends</li>
<li>Albums</li>
<li>Emails</li>
<li><a href="d.html" >Info</a></li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
Notice the data-id, it must be the same on all data-role="footer" elements to take advantage of the persistent toolbar option. The data-role="footer" element also must have the data-position="fixed" attribute.
Here is a demo: http://jsfiddle.net/SpRAA/

Resources