Jump to content

Javascript arrays


Andy-H

Recommended Posts

 

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]

Link to comment
https://forums.phpfreaks.com/topic/238973-javascript-arrays/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/238973-javascript-arrays/#findComment-1227953
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/238973-javascript-arrays/#findComment-1227961
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.