jQuery Mobile 1.4+ - preloading pages / precalculating offsets - jquery-mobile

I want to perform some operations (calculating element offsets) on a dynamically created JQM page when my app first runs. At the moment the page is built too slowly and the code returns an offset of 0 for every element. It seems that caching it might be the answer (it's a multipage JQM app with a single HTML file). Older answers to this problem on SO suggested using $.mobile.loadPage but as of 1.4 this is deprecated. The jQuery API documentation isn't the clearest on this issue - it suggests using the load method of the pagecontainer widget
$( ".selector" ).pagecontainer( "load" );
and that
The pagecontainer widget is designed to be a singleton instantiated by the framework on the body element.
I assumed this meant a pagecontainer is created automatically but if I try and run the snippet above I get an error saying I can't run pagecontainer methods before instantiating one. The JQM pagecontainer reference doesn't provide an example of how to instantiate one.
My questions are
Does preloading / caching actually render the page in the background? Will precalculating offsets even be possible?
Do I even need to use the pagecontainer load method to preload an internal page?
The docs seem to suggest that load is for loading external resources - is it relevant for internal "pages"?
Should I use something like data-dom-cache="true" instead? Or data-prefetch? These seem to be for external resources...
Would really appreciate some guidance on best practices here - the only other alternative is running a setTimeout on the offending page of the app while the code runs, but this is horrible.

Related

hashListeningEnabled - what exactly does it do?

Please can somebody explain in more detail what $.mobile.hashListeningEnabled does when enabled/disabled in jQuery Mobile?
I have tried debugging the source, but there are some bits of code I do not fully understand. And the documentation is a bit abstract (https://api.jquerymobile.com/global-config/):
jQuery Mobile will automatically listen and handle changes to the location.hash. Disabling this will prevent jQuery Mobile from handling hash changes, which allows you to handle them yourself or use simple deep-links within a document that scroll to a particular id.
The reason I ask, is because I am having problems on iOS 9 with <select data-native-menu="false"> elements. If this list too long, jQuery Mobile creates a full-screen dialog.
But it closes straight after displaying. I have been looking around and noticed somebody else has the same problem: JQuery .popup("open") not working properly on iOS 9 device.
One answer suggested seems to work OK for me - which is to set hashListeningEnabled to false:
$(document).on('mobileinit', function () {
$.mobile.hashListeningEnabled = false;
});
But I am not comfortable rolling this out to production until I understand what exactly it does.
$.mobile.changePage seems to still function.
You can have multiple pages in one html file. And you can go to "another page" even if that page is actually another div with data-role="page" in it.
http://demos.jquerymobile.com/1.4.5/pages-multi-page/
So, if you are using this type of navigation, you are screwed.
Apple guess that somebody could make an XSS attack using hash.
/pages-multi-page/#two
popups are using hash also: /pages-multi-page/#&ui-state=dialog

jQuery Mobile 1.4 Page Navigation

I need to use jQuery Mobile to build an app and I'm stuck at page navigation. I'm using jQuery Mobile 1.4. And the documentation doesn't help me very much. I have a few questions. Any help would be appreciated. Also please point out if I misunderstood anything.
I understood that $.mobile.navigate is the recommended method to change between pages in jQuery Mobile 1.4. Hoe can I use it to send data from one page to another?
What is the need for pagecontainer widget's load method? The change method itself seems to load an external page.
When I use the pagecontainer widget's change method to load an external page, it loads the page into the DOM and changes to it as expected. But, when I go back, the external page is removed from the DOM. Is there anyway to load an external page only once and let it persist in the DOM as long as the app is running?

Why does jQuery Mobile put JavaScript in the <head>, not at the bottom of the page?

Based on the jQuery Mobile docs, the <script> tags to load jQuery and jQuery mobile should go inside the <head> element.
I've always been told to load <script> tags at the bottom of the page for better performance. For example: http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5/
Is there a reason to prefer one location over the other for jQuery Mobile scripts?
Putting the <script> at the bottom is to facilitate faster loading of external elements before code executes, but in jQM nothing will really show until the pageinit/pageshow events fire, which is basically jQM's version of document.ready (everything is loaded and the DOM is there etc)
I can imagine in larger sites where you want content to be displayed ASAP this would come in handy, kind of like attaching passive on listeners even before the document.ready is another trick people use.
But all these speed optimizations don't mean much when you're waiting for a document.ready to display anything.

How to load a document in background?

I'm trying to write a Firefox extension that speeds up browsing page sequences by preloading sequence items, preprocessing them, and showing on request.
Is there any way to load and process DOM of arbitrary web page (on the same site as currently opened) in background from privileged extension code?
Ideally, the document's javascript should work as it would in a normal browser window. I suspect a hidden window would be required for this. The context on that javascript should not be privileged then.
Loading should allow user to continue normal browsing in all visible browser windows.
I don't like the idea of injecting iframes to currently opened document and making them optionally visible (the principle used by Webcomic reader userscript)
From the add-on SDK, the page-worker module might be close to what you need:
The page-worker module provides a way to create a permanent, invisible
page and access its DOM.
That said, I have no idea whether it's possible to load that invisible page into a (current or new) tab / window. You might be able to replace a current tab's document.body by the page-worker's one. Possibly. If it's legal.
You could use a lightweight browser extension to collect all links on a page onload and use link tags to prefetch the content for each, the browser will load those pages in the background: https://developer.mozilla.org/en/Link_prefetching_FAQ
OR
If you need to preload a page and have access to its DOM from extension land, you could use the Page Worker API from the Add-on SDK: https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/page-worker.html
I believe so. assuming your javascript is already running
var doc = gBrowser.selectedBrowser.contentDocument;
will get your the document of the loaded tab, you can then process it and do with it what you want. Doing it in the background and keeping the app responsive is a different story :)

JQuery Mobile, problem with loading form submitted pages

I'm working on a mobile view of our site. I'm trying to implement JQuery mobile but I'm seeing two issues that might prevent me from continuing and hoping some of you have insights.
Many pages are brought up by searches. But when the page is brought up from a search box I can't get it to load as rel='external', thus the page loads via Ajax. Great, except the pages that load are almost always larger pages broken up into data-role='page'. So when the page loads it's unable to navigate within that page, though it does hide the secondary sections. According to the documentation I need to put the rel="external" on the form, but that is not working for me. Is it possible to get the page to load without Ajax via a form submit?
Second question in same topic. In the crawl, walk, run fashion first step is a basic mobile friendly site. But I'm hoping this can also be the foundation for a PhoneGap based version of the site. From what I understand the basic model for PhoneGap will break if pages are not always loaded in Ajax as it will trigger a browser load. Since JQM requires any page with embeded data-role=page sections to be loaded via rel=external, which turns off Ajax loading, does this mean using JQM will preclude using Phone Gap to create the native client later?
Thanks in advance for any replies.
This script below should be put between your jquery min and your jquery mobile min. Otherwise it will not take effect. Adding an extension to the $.mobile with these ajax functionalities turned off may help your issue. All 3 are not needed, but they are all test worthy for your issue.
<script>
$(document).bind('mobileinit',function(){
$.extend( $.mobile , {
ajaxFormsEnabled: false,
ajaxLinksEnabled: false,
ajaxEnabled: false
});
});
</script>
Phonegap is for building phone applications, not mobile websites. It is composed Javascript which talks to specific libraries to interface with the mobile device itself.

Resources