
URL: http://www.urbandictionary.com/define.php?term=omg%20hax
Get the existing hardcoded function, convert it to a string, edit the string. Then strip the string down to just the function body, re-instantiate the string into a function, and assign it back to the onclick handler.
The code here is extremely site-specific. Here's what the original onclick function I was working against looked like:
new Ajax.Request('/publications?categories%5B%5D=1,2,3', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('6b670face4ca93ce4bf53def7685d8663780b7d9')}); return false;
The code below just changes the query string in the first parameter. The idea being that you have a bunch of checkboxes with IDs like foo-1, foo-2, and you want that query pram to reflect the number thats appended to the end of each checkbox's ID.
Not that you want to do that :) But hey, these kinds of situations sometimes come up in production. Anyway, the interesting bit is JavaScript's ability to break a function down into a string, edit the string, and then re-instantiate the string as a new function, possibly overriding the original.
Functions as data: Mmmm... Lispy.
Tested only in Firefox 3 but should work in any browser.
/* * When the button is moused over, the user is probably about to click * At that pont, recalculate the onclick method * so as to give the correct category list parameter to Rails */ Event.observe('filterButton', 'mouseover', function() { var allthecheckboxes = $$('#catfilter li input'); var selectedCats = []; var railsAjaxFn = this.onclick.toString(); allthecheckboxes.each(function(el){ if (el.checked) { var cat = el.id.match(/\d+$/); if (cat != null) { var catnumber = cat[0]; selectedCats.push(catnumber); //console.debug(el, catnumber, selectedCats); } } }); //console.debug(selectedCats); var newFnString = railsAjaxFn.replace(/\?.*?\"/, '?' + escape('[]') + '=' + selectedCats.toString() + '\"'); var fnBodyString = newFnString.match(/.*}/g)[0]; var newFn = new Function(['event'], fnBodyString + ');\n return false;'); this.onclick = newFn; console.debug(fnBodyString, newFn); //this.onclick(); });
Comments

You need to login to post a comment.
download best software to android root use app kingo or 360 for pc mac ios windows 10 8 1 7 xp computer www