javascript - How to avoid uncaught reference error - Stack Overflow

I have a mon function for many pages as belowfunction (){resetForm();$ajax call{ some code }}now i

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
Add a ment  | 

5 Answers 5

Reset to default 5

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744383783a4571561.html

相关推荐

  • javascript - How to avoid uncaught reference error - Stack Overflow

    I have a mon function for many pages as belowfunction (){resetForm();$ajax call{ some code }}now i

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信