最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript in jQuery mobile not working unless I refresh - Stack Overflow

programmeradmin0浏览0评论

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);
});

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);
});
Share Improve this question edited Jul 1, 2013 at 13:20 Ry- 225k56 gold badges490 silver badges497 bronze badges asked Jul 1, 2013 at 12:56 user2522201user2522201 751 gold badge3 silver badges12 bronze badges 3
  • 2 Please describe "doesn't work". Also, where is this using jQuery mobile? – Bergi Commented Jul 1, 2013 at 13:03
  • Dont you use .ready / $(function($) in jQuery Mobile. – Omar Commented Jul 1, 2013 at 14:06
  • @Omar - no. Pages are loaded with ajax calls (by default - can be switched off), so there is only 1 call to doc.ready. – Reinstate Monica Cellio Commented Jul 1, 2013 at 15:17
Add a comment  | 

4 Answers 4

Reset to default 11

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.

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);
});

Try pageinit like this

$(document).delegate("body", "pageinit", function() { // Use body or page wrapper id / class
    var url = window.location.search.substring(1);
    $('#mydiv').load('real_news.asp?' + url);
});

seems like nothing ever worked for me. Tried many different fixes, but i made the site too messy, that even position of certain javascript files wouldn't make the site work. Enough talk, here is what i came up with. // write it in head at top of all javascripts

<script type="text/javascript">
$(document).ready(function() {
// stops ajax load thereby refreshing page
$("a,button,form").attr('data-ajax', 'false');
// encourages ajax load, hinders refresh page (in case if you want popup or dialogs to work.)
$("a[data-rel],a[data-dialog],a[data-transition]").attr('data-ajax', 'true');
});
</script>
发布评论

评论列表(0)

  1. 暂无评论