www.webdeveloper.com
Results 1 to 7 of 7

Thread: Javascript function not getting called

  1. #1
    Join Date
    Jan 2008
    Posts
    5

    Javascript function not getting called

    Hi, I'm new to javascript, and I just wanted to share a problem that I had. It frustrated me for a while, but I eventually solved it. I couldn't find any web pages that mention this problem. Maybe some other javascript newbie will have this same problem, and maybe they'll read this post and it'll help them.

    The problem was, I was calling a function and it wasn't getting executed. Here's a simple example that reproduces the problem:


    ----test.html-----------------
    <html>
    <head>
    <script language="javascript" src="functions.js"></script>
    </head>

    <body onLoad=load();>

    </body>
    </html>
    ----End of test.html-----------



    ----functions.js--------------
    function foo1(x,y) {
    z = x+y;
    }

    function foo2() {
    foo1(3,,5);
    }

    function load() {
    alert("Hello");
    }
    ----end of functions.js------


    When test.html is loaded, no alert box pops up. So the load() function isn't getting executed, even though it's being called properly and it has no errors. But the function foo2() has a syntax error, because of the two commas in a row. So when there's a syntax error in ANOTHER function within the same js file, it seems that the entire js file gets ignored. The entire file gets ignored even if that broken function isn't being called. After fixing the syntax error in foo2(), load() can be called and executed with no problem.

    Of course, when I encountered this problem, the real code was longer and more complicated than this. So it wasn't easy for me to see that I had a syntax error in one of my functions. I didn't even bother to look in my other functions at first, because I thought that there was some problem with load(), the function that I was actually calling.

  2. #2
    Join Date
    Dec 2007
    Posts
    13
    If you install the firebug plugin for Firefox (of course you have to be using firefox), it tells you what line the errors in your javascript is at so you can see problems like this easier.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    1. language is deprecated. Use type instead
    Code:
    <script type="text/javascript">
    2. your document must have a Doctype
    3. load is reserved javascript word (it is an event/method). Never use javascript reserved words to name a function, variable, argument. Name your function otherwise, say loadMe
    4. keep the code separate from the content (call your function inside the javascript code):
    Code:
    window.onload=function(){loadMe()}
    Last edited by Kor; 01-30-2008 at 02:36 AM.

  4. #4
    Join Date
    Jan 2008
    Posts
    5
    Thanks, haku. I stated using firebug, and it's very helpful for catching errors like this.

    Thanks to you too, Kor. Your recommendations will help me avoid deprecation and ambiguity. But none of your four changes actually fixes my problem. If I make all those changes in my example, and if I keep my syntax error there, then the alert box still doesn't pop up. But still, it's good to follow standard protocols, so thanks.

  5. #5
    Join Date
    Jan 2008
    Location
    Venezuela
    Posts
    50
    <body onLoad="loadMe();">

    Otherwise use window.onload (Kor post)

  6. #6
    Join Date
    Aug 2012
    Posts
    1

    Thumbs up Thanks a ton

    Dude,

    your post served just the purpose in my case. I too am a beginner in programming as a whole.

    I had been having this same issue for hours till the last minute and was frustrated like hell. Guess what, my whole JS file was not getting called because of a single 'semicolon' in an irrelevant object. Whew...

    Thanks a ton, really.

  7. #7
    Join Date
    Jan 2008
    Posts
    5
    I'm so glad that helped you. Who would've known that someone would read my post four years later and it would solve their problem? That's the reason I made the post in the first place!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles