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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.