views:

850

answers:

5

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.

+1  A: 

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.

Jeremy Reagan
Yes I tried that before posting this question. Thanks for the useful tip nonetheless; I am even surprised at how much that can solve sometimes.
Anders
SHIFT + F5 will force-reload everything on a page, btw.
Cory R. King
Shift+F5 is not something I had even considered, thanks for the tip.
Jeremy Reagan
+4  A: 

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.

Bill James
+1 Additionally, a way to protect against this (aside from making sure your javascript works!) is to divide logical sections into separate scripts.
Joel Coehoorn
Unfortunately, the syntax is correct. Viewing the source through firebug's source view is identical (save for the dynamic content) to the source I have in VS2008.
Anders
Stick your code in JSLint just to be sure
StingyJack
That is exactly what I put it through. the only thing it complained about was the $. declaration (which is part of jQuery, which functions oddly enough).
Anders
replace the $ with "jQuery" or whatever the "proper" one is (i forget) and see if that cleans it up. I doubt it though.
Cory R. King
A: 

This may sound like a stupid question, but if you have NoScript, have you checked to make sure it's not blocking said script?

R. Bemrose
The only stupid question is no question, but I do not have any noscript tags in my code.
Anders
I meant the NoScript addon: https://addons.mozilla.org/en-US/firefox/addon/722
R. Bemrose
Ahh. No, I do not run that addon.
Anders
A: 

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.

Cory R. King
1) renamed it, same result (with new name, of course).2) validated the generated source @ http://validator.w3.org/.3) can you elaborate on wrapping?4) Tested in IE7 and FF3.5) One of my addons I cannot live without, along with FireBug :DSo far, nothing has worked :(
Anders
oops, by wrapping i mean CDATA. looks like the original comment striped it out :-)You know what else? I once it a bug where the fact the file was saved as UTF-8 pissed things off. Like it put that byte order mark in or something like that. I forget.
Cory R. King
Thanks for the clarification. Unfortunately, I am still getting this error. I hate being a programmer sometimes haha.
Anders
heh, i know the feeling. what if you rip out the code and put in a JS file? Can you create another function in the header and call that? What if you move the function somewhere else in the HTML, like the footer?
Cory R. King
I just tried putting it into its own 'scripts.js' file, in the root dir of my app. Included it just like every other JS file, and I am still getting the damn "splunge does not exist" from firebug ( i changed doLogout to splunge to make sure the name wasnt already in use )
Anders
To clarify, "just like every other JS file" means just the typical <script src="scripts.js" type="text/javascript" /> etc.
Anders
And I should also add that I've tried moving it from the <head> portion into the footer, and on top of the content as well. same result regardless of the javascript's position.
Anders
yeah, i ment shove it into an external file. Otherwise... man. Good luck :-) gotta love this stuff.
Cory R. King
yeah no kidding. thanks for your tips, ill post back here if i resolve this annoying issue.
Anders
seriously though... UTF8 and CR/LF crap. who knows. both have bit me on at least one occasion :-)
Cory R. King
i just used a JS compressor (http://javascriptcompressor.com/) on the function. When trying to call the now-compressed-function, it throws no errors but nothing happens. very strange...
Anders
A: 

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

Yisroel
It does not look like my function is defined in the DOM tab. What can I do to resolve this?
Anders
Did you check the rest of your script to make sure nothing is overriding it? I created a test page with just the code you posted. If you try that, does it work?
Yisroel
it might be more beneficial to know how and when javascript will override a particular function. I doubt that this is occurring as I changed my function's name to 'splunge', which is from RAB.
Anders
if it comes later in the script, it will override it.
Yisroel
i would try removing the comment. if the newlines aren't understood properly, the entire function is being read as if its one line. if so, your comment is getting rid of part of the code, and the function wouldn't be loaded in to the dom. alot of if's there :)
Yisroel