Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript in jQuery mobile not working unless I refresh

I'm having a jQuery mobile page with JavaScript inside. The problem is the JavaScript doesn't work unless the page is refreshed. Here is my code:

jQuery(function($) {
    var url = window.location.search.substring(1);
    $('#mydiv').load('real_news.asp?' + url);
});
like image 674
user2522201 Avatar asked Jul 01 '13 12:07

user2522201


Video Answer


2 Answers

To understand this problem you need to understand how jQuery Mobile works.

Your first problem is point where you are trying to initialize JavaScript. From your previous answers I can see you are using several HTML/ASP pages and all of your javascript is initialized form the page <head>. This is the main problem. Only the first HTML file should have JavaScript placed into the <head> content. When jQuery Mobile loads other pages into the DOM it loads only the <div> with a data-role="page" attribute. Everything else, including <head>, will be discarded.

This is because currently loaded page has a <head> already. No point in loading another pages <head> content. This goes even further. If you have several pages in a second HTML file, only the first one is going to be loaded.

I will not try to invent warm water here so here are links to my other 2 answers discussing this problem. Several solutions can be found there:

  1. Why I have to put all the script to index.html in jquery mobile (or in this blog article)

  2. Link fails to work unless refreshing

There's more then enough information there to give you an idea what to do.

The basic solutions to this problem are:

  1. Put all of your JavaScript into a first HTML/ASP file
  2. Move your JavaScript into <body>; to be more precise, move it into a <div> with data-role="page". As I already pointed out, this is the only part of a page that is going to be loaded.
  3. Use rel="external" when switching between pages because it will trigger a full page refresh. Basically, you jQuery mobile that the page will act as a normal web application.

As Archer pointed out, you should use page events to initialize your code. But let me tell you more about this problem. Unlike classic normal web pages, when working with jQuery Mobile, document ready will usually trigger before page is fully loaded/enhanced inside the DOM.

That is why page events were created. There are several of them, but if you want your code to execute only once (like in case of document ready) you should use the pageinit event. In any other case use pagebeforeshow or pageshow.

If you want to find out more about page events and why they should be used instead of document ready take a look at this article on my personal blog. Or find it here.

like image 200
Gajotres Avatar answered Nov 15 '22 12:11

Gajotres


Your question isn't exactly overflowing with pointers and tips, so I'm going with the thing that immediately sprung to mind when I saw it.

Document ready does not fire on page change with jQuery Mobile, due to "hijax", their method of "ajaxifying" all the links. Try this instead...

$(document).on("pageshow", function() {
    var url = window.location.search.substring(1);
    $('#mydiv').load('real_news.asp?' + url);
});
like image 31
Reinstate Monica Cellio Avatar answered Nov 15 '22 12:11

Reinstate Monica Cellio