Search code examples
jquerycordovajquery-mobileinitializationjquery-events

Is this a good way of handling jQuery Mobile/PhoneGap page change events?


I have this script in the head of my index.html. I want different scripts to run when different pages are shown, because I populate the pages programatically. Pages have IDs page1, page2 etc. Page-specific functions are called page1init(), page2init() etc.

document.addEventListener('deviceready', function(){
    $("div[data-role=page]").on('pageshow', function(){
        eval(this.id + 'init()')
    })

    $('#page1').trigger('pageshow')
})

Is this reasonable and does using eval() here introduce security issues?


Solution

  • Since nobody has answered, I suppose I have to default to a 'yes'? Please feel free to edit this/answer if you feel otherwise.