0

I've been designing a site for awhile now, and I've run into a stand-still.

Not sure why, but the function I made $.show_message works just fine if I call it right after it's initialized in the JavaScript file. But when I try to call it in an onclick event, or anywhere else, I get the following error:

Error: attempt to run compile-and-go script on a cleared scope

The source code is at: http://71.238.252.43/java/message.js

I can't call messages in any other fashion than by using the function at the end of the JavaScript file, and I'm getting frustrated.

I've tried turning the $. functions into normal functions, removing the (function($){ at the top, along with the ending part at the bottom })(jQuery);, and nothing seems to fix it. I'm using the latest version of FireFox and Windows 7 64 bit.

Any help on this matter would be much appreciated, as I've pretty much run out of ideas.

Jasper
  • 75,717
  • 14
  • 151
  • 146
Will
  • 13
  • 4
  • 1
    Post your code here. Post example usage (what works, what does not) also. – Tadeck Nov 15 '11 at 18:55
  • @Jasper: `onclick` event is not the same as `onClick` event ([see here for more information](http://stackoverflow.com/questions/4380719/onclick-or-onclick)). – Tadeck Nov 15 '11 at 18:56
  • Well, for example, if I add this at the end of the message.js file: $.show_message("Hello!", { title:"Caption" }); It will show the message, with the caption "Caption" just fine. But trying to call it anywhere else on my site seems to throw back that scope error. What I want to happen, is when a DIV is clicked, it will show a message. I've tried using onclick and onClick inside of the div, and the error persists. I've also tried this: $("#announcements_button").click(function() { $.show_message("Hi!"); }); Still gives the same error. – Will Nov 15 '11 at 19:03

2 Answers2

0

You can't use document.write() once the page has loaded. Instead, append an element to the DOM and write it's innerHTML:

$("<div>").appendTo("body").html("Your HTML Here");

document.write() is being called in $.check_first() which is called by $.show_message().

gilly3
  • 87,962
  • 25
  • 144
  • 176
0

You are declaring your functions inside of of the document.ready event handler, if you want the functions to be available globally (which is needed since you are declaring onclick events inline) then try attaching your function to the $.fn object: http://docs.jquery.com/Plugins/Authoring

You can also bind your events inside the document.ready event handler rather than inline.

Jasper
  • 75,717
  • 14
  • 151
  • 146