Question

The page feed.html works fine when its called from browser but when it is called from another page via a href="feed.html" page loads but the listview inside doesn't show up.

The script that works for feed.html contains $('#feedList').listview('refresh'); and it works fine when its calling from direct url.

So each time i need to refresh the page after redirection to work.

getfeed.js :

    var newsfeeds;

    $('#feedListPage').bind('pageinit', function(event) {
        getFeedList();
    });

    function getFeedList() {
        $.getJSON(serviceURL + 'getfeeds.php', function(data) {
            $('#feedList li').remove();
            newsfeeds = data.items;
            $.each(newsfeeds, function(index, newsfeed) {
                $('#feedList').append('<li><a href="feeddetails.html?id=' + newsfeed.id + '">' +
                '<img src="' + newsfeed.img + '"/>' +
                '<h4>' + newsfeed.title + '</h4>' +
                '<p>' + newsfeed.desc + '</p>' +
                '</a></li>');
            });
            $('#feedList').listview('refresh');
        });
    }

This code alone works fine but when its called from another page it does not refresh the list.

Was it helpful?

Solution

This is a wild guess but I think I know what is wrong in your case.

To understand this situation you need to understand how jQuery Mobile works. It uses ajax to load other pages.

First page is loaded normally. Its HEAD and BODY is loaded into the DOM, and they are there to await other content. When second page is loaded, only its BODY content is loaded into the DOM. So when you open your page directly and your javascript is placed inside a HEAD that javascript will execute successfully but if you are opening other html file and its javascript is in head it will be discarded as only BODY will be loaded.

If you want to find more about this problem and how to solve it (+ working solutions) take a look at my other answer: Why I have to put all the script to index.html in jquery mobile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top