Andy-H Posted June 10, 2011 Share Posted June 10, 2011 Basically, I'm trying to create a website "macro" by adding event listeners to the $(window) object and logging all click/keypress events but I can't seem to master javascript arrays. All I want to do is create a dynamic length array so I can log every instance of a click/key event in an array. $(document).ready(function() { var events = { 'click' : [ "pageX","pageY" ], 'keypress' : [ "keyCode" ] }; var eventLog = []; var i = 0; $.each(events, function( eventType, eventData ){ $(window).bind(eventType, function(e) { $.each(eventData, function(k, data) { eventLog[i] = []; eventLog[eventType] = []; eventLog[eventType] = data; eventLog[eventType] = eval('e.' + data) ]; }); i++; }); }); $('.testing').click(function() { $.each(eventLog, function(k,v) { $.each(v, function(key,val) { alert(val); }); }); }); }); [/i] Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 10, 2011 Author Share Posted June 10, 2011 Got to this: $(document).ready(function() { var events = { 'click' : [ "pageX","pageY" ], 'keypress' : [ "keyCode" ] }; var eventLog = []; var i = 0; $.each(events, function( eventType, eventData ){ $(window).bind(eventType, function(e) { $.each(eventData, function(k, data) { eventLog[i] = []; eventLog[eventType] = []; eventLog[eventType][eventLog[eventType].length+1] = data; eventLog[eventType][eventLog[eventType].length+1] = eval('e.' + data); alert(data); alert(eval('e.' + data)); }); i++; }); }); $('.testing').click(function() { alert(eventLog[0]['click']); }); }); [/i] But it still wont store the pageX value, I get an alery of ,,pageY,345 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 10, 2011 Author Share Posted June 10, 2011 Fixed code: $(document).ready(function() { var events = { 'click' : [ "pageX","pageY" ], 'keypress' : [ "keyCode" ] }; var eventLog = []; var i = 0; $.each(events, function( eventType, eventData ){ $(window).bind(eventType, function(e) { var j = 0; eventLog[i] = []; eventLog[eventType] = []; $.each(eventData, function(k, data) { eventLog[eventType][j++] = data; eventLog[eventType][j++] = eval('e.' + data); }); i++; }); }); $('.testing').click(function() { alert(print_r(eventLog)); }); }); function print_r(theObj){ if(theObj.constructor == Array || theObj.constructor == Object){ document.write("<ul>") for(var p in theObj){ if(theObj[p].constructor == Array|| theObj[p].constructor == Object){ document.write("<li>["+p+"] => "+typeof(theObj)+"</li>"); document.write("<ul>") print_r(theObj[p]); document.write("</ul>") } else { document.write("<li>["+p+"] => "+theObj[p]+"</li>"); } } document.write("</ul>") } } [/i] outputs: [0] => object[click] => object[0] => pageX[1] => 612[2] => pageY[3] => 439[1] => object[click] => object[0] => pageX[1] => 856[2] => pageY[3] => 461[2] => object[keypress] => object[0] => keyCode[1] => 108 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.