最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Why is my javascript function not found by the page it is embedded in? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Mar 2, 2009 at 22:02 Anders asked Mar 2, 2009 at 16:14 AndersAnders 12.6k36 gold badges101 silver badges146 bronze badges 2
  • Have you checked this is not a cache issue, i.e. the browser holding an old version of your page HTML? – REA_ANDREW Commented Mar 2, 2009 at 16:17
  • Yes, that was one of the first things I checked. – Anders Commented Mar 2, 2009 at 16:31
Add a comment  | 

8 Answers 8

Reset to default 5

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.

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.

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.

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

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.

If you are using DevExpress controls these links may help you: How to register and execute a JavaScript downloaded to the client via a callback and How to register and execute a JavaScript downloaded to the client via a callback (standalone JS file) and Executing javascripts from user controls dynamically created through ASPxCallback panels

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

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.

发布评论

评论列表(0)

  1. 暂无评论