Siyah
Siyah

Reputation: 2897

How to get onload function working in Ionic?

I am having some troubles here:

function load(){
    var elements=document.getElementsByClassName('inputfield');
        for(let i=0;i<elements.length;i++){
            elements[i].addEventListener("keydown",keydown);
        }
  }

window.onload = load;

This is my JavaScript which works perfectly fine with this HTML outside my Ionic app:

        <input class="zipCode inputfield" />

Yet, it is not working in Ionic! I have no errors in the console or whatsoever...

I tried this too:

<body onload="load();">
   <input class="zipCode inputfield" />
</body>

But it didn't do the trick either.

I am out of options, as this is perfectly valid JS. How can I fix this?

Upvotes: 0

Views: 2078

Answers (3)

Marc
Marc

Reputation: 14323

The following works fine in Ionic v8:

window.addEventListener('load', load);

Upvotes: 0

Chaoyenpo
Chaoyenpo

Reputation: 389

document.addEventListener("deviceready", load, false) ;

function load(){
    var elements=document.getElementsByClassName('inputfield');
    for(let i=0;i<elements.length;i++){
        elements[i].addEventListener("keydown",keydown);
    }
}

Upvotes: 0

coder
coder

Reputation: 8712

Because of you are working with ionic better to use view life cycle and events . There are number of events you could find in their document

eg.

 $scope.$on('$ionicView.enter', function () {
            //do your work here..
        })

Upvotes: 0

Related Questions