Anders
Anders

Reputation: 12560

Why is my javascript function not found by the page it is embedded in?

I have a page that has a simple javascript in the header portion of the page:

<script type="text/javascript">
    function doLogout() {
        var conf = confirm("Really log out?");
        if (conf === true) {      //changed == to === for boolean comparison
            $.post("logout.aspx");
        }
    }
</script>

It uses jQuery to do an AJAX post to my logout page. The only issue right now is that when I click on the link (<a href="#" onclick="doLogout();">logout</a>) to fire this function, nothing happens. I checked FireBug's console, and it told me that the function is not defined. This has happened to me before, but I think I botched a bunch of code to fix it sometimes.

Does anyone know the proper way to fix this issue?

Edit

After doing a lot of googling and trying different things, I found this very concise and informative post. Apparently, as the linked article states, the way the script is referenced in the web site is important as it won't run properly otherwise! Hopefully this information will be useful for more people.

Upvotes: 4

Views: 28893

Answers (8)

Marco Rodrigues
Marco Rodrigues

Reputation: 31

I had this issue and discovered the problem was just a wrong case letter inside the name.

Call: filterCheckbox()

vs

function filterCheckBox() {}

problem: lowercase "box" vs uppercase "Box".

So check if the name is exactly the same.

Upvotes: 0

Powerlord
Powerlord

Reputation: 88806

The issue might occur if you have NoScript. You should check and make sure it's not blocking said script.

Upvotes: 0

Rox
Rox

Reputation: 339

I had the same issue and tried all that's been suggested here without success.

The only way I fixed it was by discovering that in the <script src="jquery.js"> tag I was using in the head of the page I forgot to close it with its </script> causing the page to ignore all Javascript functions. So please check that your includes look like:

<script src="jquery.js"></script>

I hope that helps. Ross.

Upvotes: 0

Cory R. King
Cory R. King

Reputation: 2796

Things to test:

1) Can you call this function from something else? Like add a <script> at the bottom of the page to call it?

2) Does the page validate? Sometimes I get screwy javascript errors if there is some busted HTML like a missing </b>

3) I've been starting to wrap my javascript in <![CDATA[ ]]> just incase I've got goofy chars in my javascript.

4) I assume you've tested this in other browsers and have the same behavior, right?

5) If you haven't installed it already, install the Web Developer firefox addon. It has a nifty toolbar menu that will disable the cache for you so everything reloads.

6) As weird as it sounds, I once hit a javascript issue that was because of how my text editor was saving UTF-8 files. I forget the details, but it was adding some byte-order-mark or something that upset the browser.

Upvotes: 2

Yisroel
Yisroel

Reputation: 8174

Other ideas for you to test:

  1. is the function defined in the DOM tab in FireBug?

  2. if you call doLogout() from the FireBug console, what happens?

  3. I assume this is not the only script on that page. Make sure that some later script is not modifying doLogout to something else

Upvotes: 0

billjamesdev
billjamesdev

Reputation: 14640

This can also occur if there is a syntax error earlier in your javascript code. Often this will just be interpreted as the function not existing (nor any function AFTER the error). Check the code above this code (if there is any) and this code for syntax errors.

A way to tell if the cache error is it is to open Firebug and view the Script source. If the page was cached, you won't see your code. If it loaded but has syntax errors, the code will show, though it won't "find" it.

Upvotes: 5

user18411
user18411

Reputation:

I've had this occur when the page had been cached and so it didn't load the new script in. So to fix it clear all private data from Firefox. Not sure if that helps but it sure happened to me a bunch.

Upvotes: 1

Related Questions