1

I have studied and tried 3 different solutions, but haven't been able to get past the annoying error :

Uncaught ReferenceError: SetupRichTextAndTags is not defined

Situation :

I am populating a hiddenfield with data (C# back-end), this is purely HTML which i will use to populate a SummerNote rich-text field by calling the following javascript :

$(".summernote").code("your text");

My tries at RegisterStartupScript :

//ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { SetupRichTextAndTags(); });", true);
//ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>SetupRichTextAndTags();</script>", false);
ScriptManager.RegisterStartupScript(Page, GetType(), "SetupRichTextAndTags", "<script>SetupRichTextAndTags()</script>", false);

All of these gives me the error...

The script itself is within an included javascript file in the aspx page, and i think that might be the issue.. But.. i have not found any solutions to how to actually fix that..

Any tips ?

Terje Nygård
  • 1,233
  • 6
  • 25
  • 48

1 Answers1

4

The JavaScript function SetupRichTextAndTags is not available on the page when your registered scripts run.

Before you can call the function you need to load it into the page. You can declare the function in a client script block but then you have to write the JavaScript into the C# code which is not easy to work with. Instead you can declare the functions in a normal JavaScript file and then load that into the page.

Here is a template, note that you check if the script blocks are registered so they don't get added again if there is a post back.

ClientScriptManager csm = Page.ClientScript;

// this registers the include of the js file containing the function
if (!csm.IsClientScriptIncludeRegistered("SetupRichTextAndTags"))
{
    csm.RegisterClientScriptInclude("SetupRichTextAndTags", "/SetupRichTextAndTags.js");
}

// this registers the script which will call the function 
if (!csm.IsClientScriptBlockRegistered("CallSetupRichTextAndTags"))
{
    csm.RegisterClientScriptBlock(GetType(), "CallSetupRichTextAndTags", "SetupRichTextAndTags();", true);
}
Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
  • Thanks @Dave :) I'll give it a shot riiiight away :) I'll keep you posted on the results :) – Terje Nygård Jan 18 '16 at 23:30
  • Got a bit further it seems :) But in some way it seems that this is the absolute first javascript that's loaded... So this means i have to add all the script files neccessary for handling this function's dependencies ? (eg : Jquery, SummerNote.js, etc...) ? Error : Uncaught ReferenceError: $ is not defined(anonymous function) @ CreateArticle.aspx.js:1 CreateArticle?type=club&mode=edit&id=35:59 Uncaught ReferenceError: SetupRichTextAndTags is not defined – Terje Nygård Jan 18 '16 at 23:41
  • Yes, that's why with jQuery the initial entry point to the code goes into the `$(document).ready` call. This ensures the page has finished loading all the script dependencies. You can add the scripts you need to the aspx page, use bundles with MVC or a dependency loader like RequireJS. See http://stackoverflow.com/questions/950087 – Dave Anderson Jan 18 '16 at 23:46
  • Aah.. understand :) but since the script is actually stopping on $(document).ready(function () { due to not finding JQuery... i have to include the jquery as a csm.RegisterClientScriptInclude("JQuery", "/path/jqueryxxxx.js" right ? – Terje Nygård Jan 18 '16 at 23:50
  • Yes that would work but also look at using a CDN; http://www.asp.net/ajax/cdn for jQuery. If you have an aspx page it is easier to drop the script tag in at the bottom of the page rather than using the Script Manager. – Dave Anderson Jan 18 '16 at 23:54
  • Seems like that did the trick, but it still cannot find the actual script function : – Terje Nygård Jan 18 '16 at 23:55
  • I assume function `SetupRichTextAndTags` is declared in `PageScripts/CreateArticle.aspx.js`? The page is getting to the call before the function loads. Since you have jQuery you should register `$(document).ready(function() { SetupRichTextAndTags(); })` so it will wait for the script to have loaded. – Dave Anderson Jan 18 '16 at 23:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101023/discussion-between-terje-nygard-and-dave-anderson). – Terje Nygård Jan 19 '16 at 00:00