I have a mon function for many pages as below
function (){
resetForm();
$ajax call
{
// some code //
}
}
now if i have not written the resetForm() fn in correspoding js, its giving the uncaught reference error as it should give and also my code followed after than line is not executed.
The js files where i have written resetForm fn() is working fine, but others are not.
The solution i have now is writing a blank resetForm function in other pages. Any other way ? thanks for the help
I have a mon function for many pages as below
function (){
resetForm();
$ajax call
{
// some code //
}
}
now if i have not written the resetForm() fn in correspoding js, its giving the uncaught reference error as it should give and also my code followed after than line is not executed.
The js files where i have written resetForm fn() is working fine, but others are not.
The solution i have now is writing a blank resetForm function in other pages. Any other way ? thanks for the help
Share Improve this question edited Apr 29, 2014 at 11:57 Amit Sirohiya 3331 silver badge13 bronze badges asked Apr 29, 2014 at 11:52 pnagapnaga 212 silver badges5 bronze badges 3-
I'm not entirely sure what you are trying to acplish here. You want to call
resetForm()
only if it has been defined before, but ignore it otherwise? – mikezter Commented Apr 29, 2014 at 11:55 - It seems resetForm it's not defined by the time that code is executed. You need to reference the file containing that function before using it. – Claudio Redi Commented Apr 29, 2014 at 11:55
- do you need that function be actually executed at that point? – fast Commented Apr 29, 2014 at 11:57
5 Answers
Reset to default 5You can check with typeof
if (typeof resetForm === 'function') {
resetForm()
}
// carry on.
This checks to see if you have a function called resetForm
available and, if you do, executes it.
you can check it it exists (globally) and only than call it by:
window.resetForm && resetForm();
Here's a few obvious options:
- Write a blank function like you did.
typeof resetForm === 'function' && resetForm();
- Make sure to load only code that is relevant to the current page by better modularizing your code.
just check if resetForm
is defined:
if (resetForm) {
resetForm();
}
I know it's not jquery-tagged question, but consider using jquery's onReady handler, it is executed when all javascript is loaded.
//main.js
$(function(){
resetForm();
});
//functions.js
function resetForm() { .. }
It will call resetForm
properly event if resetForm
is in separate js and loaded after main.js
is loaded.