0

Hi I'm exucuting the following code and I kept the following code in separate js file and referencing that file in all other .aspx files. When I put following code in .aspx file script section directly it's working properly but when I kept it in separate js file along with other functions it's not working properly

function pageLoad(sender, args) {

  var dict = $.parseJSON('<%=DictJson %>');
var data = eval(dict);

}

I'm getting error at var dict = $.parseJSON('<%=DictJson %>');. The error being displayed is Microsoft JScript runtime error: Exception thrown and not caught and it is displaying that throw in json file } if (!v("json-parse")) { var M = String.fromCharCode, N = { 92: "\", 34: '"', 47: "/", 98: "\u0008", 116: "\t", 110: "\n", 102: "\u000c", 114: "\r" }, b, A, j = function () { b = A = w; throw SyntaxError(); }, q = function () { for (var a = A, f = a.length, c, d, h, k, e; b < f; ) { e = a.charCodeAt(b); switch (e) { case 9: case 10: case 13: case 32: b++;

I wrote the DictJson is a parsed dictionary I'm parsing it in back end in some other base page where it can be accessed by each and every page. The parsing code is

    public string DictJson
    {
        get
        {

            MouseOverFieldDict mdict = (MouseOverFieldDict)(HttpContext.Current.Session["MouseOverFieldDict"]);
            JavaScriptSerializer jSer = new JavaScriptSerializer();
            return jSer.Serialize(mdict.FieldDict);

        }
    }

Please help me with the error. I don't get it why it is throwing that error in script

0

2 Answers 2

1

The server-side expression "<%=DictJson %>" is only evaluated in the .aspx file if DictJson is an accessible member (property/field) in the code-behind. The request for the JavaScript file is a completely separate request, so it is not aware of DictJson.

You can still have your function in your JavaScript, but I would suggest something like this:

JavaScript file:

function myPageLoad(DictJson) {
  var dict = $.parseJSON(DictJson);
  var data = eval(dict);
}

Code Behind (roughly, I'm not set up for c#):

void Page_PreRender(object sender, EventArgs args) {
  string scriptName;
  string scriptText;

  // have jQuery to call the function in your JS with the serialized Dict
  scriptText = "$(function(){myPageLoad('" + DictJson + "');});";
  scriptName = "onLoadScript";

  ClientScriptManager.RegisterStartupScript(Me.GetType(), scriptName, scriptText, True);
}

RegisterStartupScript documentation: http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

i want to maintain all the script in separate script file not in the script part of the aspx file. Then I should be able to refer this script file in all other aspx files. In this how can we make DictJson to be accessible in each and every page?
You can do this by adding a new Master Page to your Web project, and declaring your property/PreRender event handler in the code-behind for it. Next, in the .aspx files, change the MasterPageFile attribute of the Page declaration to point at the new Master Page. :)
0

If I am not mistaken, the issue is that when you move the javascript into its own separate file, javascript does not know how to natively handle the "gator" tags (<%=...%>). You would either need to leave your script inline in your aspx page or would need to come up with a mechanism to pass into your function call the value of DictJson when you make the function call.

1 Comment

can you Please suggest a way to pass DictJson value during function call?

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.