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 ?

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Dave :) I'll give it a shot riiiight away :) I'll keep you posted on the results :)
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
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 stackoverflow.com/questions/950087
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 ?
Yes that would work but also look at using a CDN; 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.