faqts : Computers : Programming : Languages : JavaScript : Links

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 39 people (56%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How to submit a javascript function throught an http request?

Jun 12th, 2002 07:35
David Blackledge, Anne Durand,


Interesting question... I'm not really sure why you'd want to do this, 
but you could do it like so:
First, define your function in the current page
function myFunc(arg1, arg2) {
 alert(arg1+arg2);
 return false;
}
then, take advantage of the escape and .toSource functions:
window.location.href = "blah?submitfunction="+escape(myFunc.toSource());
on the other page (blah) do something like:
// extract url value
var funcdef = 
window.location.search.substring(window.location.search.indexOf("submitf
unction=")+"submitfunction=".length)
// unescape url value
funcdef = unescape(funcdef)
// run value
eval(funcdef);
// try it.
myFunc("yay!","JavaScript!");
David.
http://David.Blackledge.com