javascript - Problems loading external script that worked when I loaded it from my view -


the script sat on ejs view tucked in between nice script tags. moved external file , did not work @ all, on click listener did nothing. moved down bottom of page right outside of body tag , allowed work , when did work slower before. first time have used pageinit instead of document.ready, assuming why can not figure out problem is. here code. help.

var currentlist; var elementid; var id;  $('#mowingmaster').on('pageinit', function (event) {      $('li').each(function (index) {          var elementid = $(this).attr('id');         elementid = '#' + elementid;          $(function () {             $(elementid).click(function (event) {                  var elementid = $(this).attr('id');                 id = elementid;                 elementid = '#' + elementid;                  setelementid(id);                 $.mobile.changepage("#dailylist");             });         });     });      $("#dailylist").on("pagebeforeshow", function (event, ui) {         $("#testhide").hide()         setcurrentlist(elementid);     });      $("#dailylist").on("pageshow", function (event, ui) {      }); });  function setelementid(id) {     id = id; }  function setcurrentlist() {      var currentlist = id;      $.ajax({         type: "post",         url: "/scape/mowinglist",         data: {             currentlist: currentlist         },         success: function (data) {          }     }); }; 

this first time have used pageinit instead of document.ready

$('#mowingmaster').on('pageinit', function (event) { 

for work, element #mowingmaster must exist when snippet run – if doesn’t, selector returns nothing, there nothing bind events on.

document.ready used differently – use start executing code when document ready, , bind events within code.

please @ the docs pageinit:

$( document ).on( "pageinit", "#aboutpage", function( event ) { 

they binding event document here , provide selector element should done (#aboutpage) – , that’s way should use well. (document “always” available right start, whereas dom elements might not exist @ point script executes.)


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -