Jquerymobile - $ .mobile.changepage

I want to open a .html file from my .js file. So I used $ .mobile.changePage ("file.html"). There is a .js file in file.html. But the .js file is not called when file.html is called.

Extremely important.

first.js

$.mobile.changePage ("file.html"); 

file.html

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>jQuery Mobile Framework - Dialog Example</title> <link rel="stylesheet" href="jquery.mobile-1.0a2.min.css" /> <script src="jquery-1.4.4.min.js"></script> <script src="jquery.mobile-1.0a2.min.js"></script> <script src="../Scripts/file.js"/> // Could not imported <script src="../Scripts/helperArrays.js"/> // Could not imported <script src="../Scripts/globalVariables.js"/> // Could not imported </head> <body> <div data-role="page"> <div data-role="header" data-nobackbtn="true"> <h1>Vaults</h1> </div> <!-- <div data-role="content" data-theme="c" id="contentVault"> <a href="Views/searchPage.html" data-role="button" data-theme="b">Sample Vault</a> <a href="Views/searchPage.html" data-role="button" data-theme="c">My Vault</a> </div> --> <div data-role="content" id="content"> <ul id="listview" data-role="listview" data-inset="true" data-theme="e" data-dividertheme="b"> <li id="listdiv" data-role="list-divider">List of Items</li> </ul> </div><!-- /content --> </div> </body> </html> 

Please help me..

+5
jquery-mobile
Dec 22 '10 at 9:55
source share
2 answers

JQuery mobile receives pages through AJAX and adds their contents to the current page. I saw some notifications about changing the name of the page on the incoming, so they (planning?) Turn to the head, but at the moment jquery mobile does not seem to load external js when the page loads.

More importantly, if you use $ (document) .ready (), it will not run because it is AJAX

+4
Dec 22 2018-10-22
source share

When jQM loads another page via AJAX, it only pulls something into your div [data-role = "page"], and nothing else, like head


So, you can, if you want, include any JS / CSS in this div, the problem is that if this page is available several times, then some CSS will accumulate, but the problem is much worse for JS.

Basically you get every page added to the DOM, so JS works on the whole DOM (every page you upload) if you use a global selector like $ ('div.someClass'), even using the id isn’t a perfect solution, because if you can link to one page twice.


For small sites

I solved this by moving all the CSS to a single file and for the JS code that I want to run every time the page loads, I get attached to the jQM events on the page and jQM page:

 <script type="text/javascript"> $("div:jqmData(role='page'):last").bind('pageinit', function(){ //your code here - $.mobile.activePage not declared }); $("div:jqmData(role='page'):last").bind('pageshow', function(){ //your code here - $.mobile.activePage works now }); </script> 

The pageinit event is fired when the page loads only once (after loading it, it remains in memory if you go to it, even with the back button it does not fire again), on the other hand, every time the page is displayed, even when you navigate to it using the back button in a browser, for example.

The pageinit function is triggered when the DOM is present, the showhow event is only if you have code that depends on the displayed DOM, and you need a quick link to the active page via $ .mobile.activePage or some data is changed and you need to update this information each times when it is displayed. For most purposes, I use only pageinit as document.ready for jQM and bind my events there. Use binding for static elements and on instead of live elements for dynamic elements, because live events are listened to in the document root directory, you want to listen to the div page.



For larger sites

For larger sites, there are advantages to linking a live event to any pages and processing page types, so the js code does not load more than once.

If you have external js files with support for auxiliary functions that you need only once, put this at the top of all your pages (if there are not many), if you had a very large site, you could probably do better tracking which JS files were uploaded to the server.

All this can be avoided by simply not using AJAX to load new pages, so consider whether this transition / load effect is worth it.


* Here I process large jQM sites: *

  • Bind a live event to all pages / dialogs. pageinit and pageshow events:

$(document).on('pageinit pageshow', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function(event){

  • I link to every page with the name: <div data-role="page" data-mypage="employeelist">
  • In this live event, you can basically have a switch statement for each page "name", and then check event.type for pageinit / pageshow or both and put your code there, and then every time a page is created / this event will be fired , he knows which page called it, and then calls the corresponding code

  • In the end I had too much code, so I built a handler object where each js page is included in a separate js file and can register handlers with a live event

The disadvantage is that all your js for your entire site are loaded on the first page that the user reaches, but the minimized even large site is smaller than jQuery or jQM, so this should not be a concern. The advantage is that you no longer download all your JS for each page through AJAX every time a user navigates to a new page.

* note: now RequireJS can make this even more manageable

+7
Jan 31 '12 at 18:38
source share



All Articles