Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JavaScript after a page is transitioned with jQuery Mobile

When a page do a transition to another (and use the location.hash stuff) , this second page does not load any JavaScript. How can I execute JavaScript when a page is loaded (if this one "comes" from a transition from its parent) ?

I have page1.html, with a link to page2.html. This page2.html loads with a transition (slide effect by default).

In page2.html no JavaScript is executed. I tried with a simple

<script type="text/javascript">
alert("x");
</script>

but does not work. I tried with a lot of document.ready, bind, live, oncreate, pagecreate, and so on.

like image 743
FlamingMoe Avatar asked Mar 16 '11 22:03

FlamingMoe


People also ask

What are the ways to execute JavaScript after page load?

Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.

Can I use JavaScript and jQuery on same page?

Projects In JavaScript & JQueryYes, you can use multiple versions of jQuery on the same page. To avoid any kind of conflict, use the jQuery.

Is jQuery Mobile outdated?

jQuery Mobile is no longer supported.

Can you combine jQuery and JavaScript?

Can you combine jQuery and JavaScript? It is possible to run jQuery and JavaScript together in you application. All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes.


2 Answers

You could use a callback function. After the animation call a function.

in jQuery:

$('#yourElement').animate({
    opacity: 0.25,
  }, 5000, function() {
    // Animation complete. add your callback logic here
});

If using Webkit transitions:

$('#yourElement').addEventListener(
 'webkitTransitionEnd',
 function( event ) {
     // Animation complete. add your callback logic here
 }, false );

And if using jQuery Mobile, it appears you can use the 'pageshow' and 'pagehide' event listeners:

http://jquerymobile.com/test/docs/api/events.html

$('div').live('pageshow',function(event, ui){
  alert('This page was just hidden: '+ ui.prevPage);
});

$('div').live('pagehide',function(event, ui){
  alert('This page was just shown: '+ ui.nextPage);
});
like image 162
DA. Avatar answered Sep 25 '22 00:09

DA.


Basically in Jquerymobile when you do a page transition only the content inside

    <div data-role="page">
    ...
    </div> 

gets changed so effectively all your scripts and everything you have included in

    <head></head> 

tags is not getting loaded.

So to solve this you need to include your script inside the page, like below

    <div data-role="page">
    ...
    <script type="text/javascript" src="yourScript.js"></script>
    </div>

Thanks, Dinesh Parne

like image 34
Dinesh Reddy Parne Avatar answered Sep 22 '22 00:09

Dinesh Reddy Parne