2

I'm really hoping someone can help me out with this. I have been experimenting with jQuery Mobile and have run into a problem. The problem is regarding a jQuery lightbox called Swipebox. The plugin itself works fine to a point so I'll highlight when it doesn't work

The CSS and script are situated in the head of my document. The call script is situated inside the body, but inside the tag.

The only time it doesn't work is if the page itself acts as the 'first page'. On the initial load, it works. But then if I navigate away from that page and return via AJAX loading, then the plugin stops working.

I don't know an awful lot about jQuery Mobile at the moment but I assuming that when the 'first page' is loaded for the first time (or refreshed possibly), it gets cached. Hence why when returning to the first page, from another page, the loading time is instant. But unfortunately it seems to totally break or not include the script.

Is anyone able to point me in the right direction with this. A basic layout of my markup is below. Any advice would be much appreciated

<!DOCTYPE html> 
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">    

    <link rel="stylesheet" href="/templates/assets/css/mobile/swipebox.css" />
    <script src="/templates/assets/js/mobile/jquery.swipebox.js"></script>
</head> 

<body>

<div data-role="page" class="padded-bottom" data-dom-cache="false">

<section data-role="content">


<!--Gallery images-->


</section><!--content-->


<script>
$(".swipebox").swipebox({hideBarsDelay : 0});
</script>

</div><!-- page -->

</body>
</html>

At the moment I have the following in 'Template 1'

<script>

$('#hotel').on('pageload', function() {
$(".swipebox").swipebox({hideBarsDelay : 0});
$(".test").text("jQuery works");
});

</script>

and this for 'Template 2'

<script>

$('#hotel-view').on('pageinit', function() {
$(".swipebox").swipebox({hideBarsDelay : 0});
$(".test").text("jQuery works");
});

</script>

This makes the plugin work perfectly on 'Template 2' regardless of how its loaded. But the plugin is also being used on 'Template 1'. Under the 'Insider' tab. It seems the plugin cannot work perfectly on both pages. The problem has progressed rather than been solved :)

29
  • As far as I'm aware if you load using Ajax which is when the call will not work. I assume this being due to the fact that onload the query binds to the object where as with Ajax the query loads then the function loads which means the function will not bind to the jquery. I believe to get this to work with Ajax you will need to use live or bind. Commented Dec 12, 2013 at 22:00
  • Are you using multi-page model (multi html)? Gallery page isn't the first page? If yes, js library and function should be placed inside page div. Commented Dec 12, 2013 at 22:08
  • Thanks for your input guys. @liam-sorsby so I should keep the call where it is and try firing it from inside a .live or .bind function? Commented Dec 12, 2013 at 22:38
  • @omar yes I am using multi-page and the gallery page in question is not the first page. But if the galley page is refreshed at some point then the problem becomes a possibilty, because it then becomes the 'first page'. Commented Dec 12, 2013 at 22:41
  • It won't work when refreshed because jquery and JQM aren't loaded in head. This is one of multi-page model problems. JQM works much better in single-page model. In fact, it's meant to be used as single-page model. To solve your problem, all html files should contain all CSS links and libraries, extra functions go inside page div. Commented Dec 12, 2013 at 22:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.