Harsimran Singh
Harsimran Singh

Reputation: 738

Execute Javascript function oninit in Framework7 on index page - Cordova

On the app start I want to execute a JavaScript Function.

I tried to work on this with index as having - data-page="index" and using below function:

myApp.onPageInit('index', function (page) {
  // "page" variable contains all required information about loaded and initialized page 
})

But this function doesn't work when the application is just started.

Upvotes: 1

Views: 5141

Answers (3)

Nikollai Hernandez
Nikollai Hernandez

Reputation: 97

Try to change

$$(document).on('deviceready', function() {
    // Your content here
});

For

$$(document).on('DOMContentLoaded', function(){
     // Your content here
});

Upvotes: 0

Petroff
Petroff

Reputation: 828

You have to trigger it.

myApp.onPageInit('index', function() {
    // Code here
}).trigger();

Use .trigger() only for index route. You can read more in framework7 documentation.

Update

You can also check the issue conversation in framework7 github repo.

Upvotes: 1

Naitik
Naitik

Reputation: 1465

YOu can try this:

window.onload = function(){   document.addEventListener("deviceready", onDeviceReady, false); }

function onDeviceReady() { // your device ready logic here }

Edit:

We can do this in pure JavaScript (although this will not work in all browsers):

var doSomething = function (event) { . . . };

window.addEventListener('DOMContentLoaded', doSomething);

But we can do it more easily with jQuery, and it will work cross-browser:

$(window).ready(doSomething);

This can be shortened further to:

$(doSomething);

In all the above examples, doSomething is a JavaScript function.

Upvotes: 0

Related Questions