Forum Moderators: open
I have few forms where I want to use the same javascript functions
eg first form is called 'edit', second 'load':
html
<script language="JavaScript" src="cookie.js"
type="text/javascript"></script>
<script language="JavaScript" src="filtr.js"
type="text/javascript"></script>
</head>
<body onLoad="java_getFiltr();">
<form name="edit" action="edit.html" method="POST">
<input name="a" type="text">
<input name="b" type="text">
<button onclick="java_saveFiltr()" >Filter</button>
cookie.js - a file to set, get and delete cookies
filtr.js - file with desired functions:
function java_saveFiltr()
{
var f = document.edit.a.value + "%2" +
document.edit.b.value + "%2";
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("filtr", f, now);document.edit.submit();
}
function java_getFiltr()
{
var begin = 0;
var end = 0;
var f = new Array(15);
if(document.cookie.indexOf("filtr=")!= -1)
{
var str = getCookie("filtr");
for(i = 1; i <= 26; i++)
{
end = str.indexOf("%2", begin);
f[i] = str.substring(begin, end);
begin = end + 2;
}document.edit.a.value = f[1];
document.edit.b.value = f[2];
}
document.edit.a.select();
}
The question is: what should I write in the function and where I call them, to use the function with different forms.
I have also forms: <form name="load", <form name="unload", <form name="report", And I would like to use the same filter to all of them.
The only way for now is to copy the file filtr.js into a new one, where I change all the "edit" into "load" and it's working.
However I would like to do something like this:
<body onLoad="java_getFiltr(edit);">
Thanks for any answers.
PS. if there's a simpler method to store arrays in one cookie I'm open to suggestions, btw I heard about getElementById(), but don't know why I don't use it. Sorry
Best regards!
Michal Cibor
<script language="JavaScript" src="cookie.js"
type="text/javascript"></script>
<script language="JavaScript" src="filtr.js"
type="text/javascript"></script>
</head>
<body onLoad="java_getFiltr();">
<form name="edit" action="edit.html" method="POST">
<input name="a" type="text">
<input name="b" type="text">
<button onclick="java_saveFiltr()" >Filter</button>
filtr.js:
frm=document.getElementsByTagName("form");function java_saveFiltr(d)
{
var f = frm[d].a.value + "%2" +
frm[d].b.value + "%2";
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("filtr", f, now);
frm[d].submit();
}
function java_getFiltr(d)
{
var begin = 0;
var end = 0;
var f = new Array(15);
if(document.cookie.indexOf("filtr=")!= -1)
{
var str = getCookie("filtr");
for(i = 1; i <= 26; i++)
{
end = str.indexOf("%2", begin);
f[i] = str.substring(begin, end);
begin = end + 2;
}
frm[d].a.value = f[1];
frm[d].b.value = f[2];
}
frm[d].a.select();
}
<body onload="java_getFilter(0)">