/ Published in: JavaScript
Options:
1. Write the desired values into a hidden field that contains json. Reference the hidden field by ID and parse the json to get the desired vars.
2. Call a webmethod that returns the values in json format
3. Write js via the code-behind that writes the variables to the page in a separate script tag. The js is placed inline in the page. Call: Page.ClientScript.RegisterClientScriptBlock. See example below.
So why is putting the entire javasscript in a string within the code-behind a lousy idea?
1. No code coloring
2. No syntax checking
3. Have to escape quotes which makes it hard to read and adds work
4. JS isn't cached since it's a string sitting within the html, unlike an attached .js file.
5. No separation of concerns. Client script should ideally be in a .js file to keep server side business logic from being intermingled with other concerns.
6. Inability to reuse. A useful .js file can be called from multiple pages. js stored in a string of a code-behind is page specific.
7. No IDE assistance for formatting or intellisense support
1. Write the desired values into a hidden field that contains json. Reference the hidden field by ID and parse the json to get the desired vars.
2. Call a webmethod that returns the values in json format
3. Write js via the code-behind that writes the variables to the page in a separate script tag. The js is placed inline in the page. Call: Page.ClientScript.RegisterClientScriptBlock. See example below.
So why is putting the entire javasscript in a string within the code-behind a lousy idea?
1. No code coloring
2. No syntax checking
3. Have to escape quotes which makes it hard to read and adds work
4. JS isn't cached since it's a string sitting within the html, unlike an attached .js file.
5. No separation of concerns. Client script should ideally be in a .js file to keep server side business logic from being intermingled with other concerns.
6. Inability to reuse. A useful .js file can be called from multiple pages. js stored in a string of a code-behind is page specific.
7. No IDE assistance for formatting or intellisense support
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var i = 4;</script>"); //A more involved example sending down a list of objects in json: public class Employee { public string Name { get; set; } public string ID { get; set; } public string Age { get; set; } } Employee oEmployee1 = new Employee { Name = "Pini", ID = "111", Age = "30" }; Employee oEmployee2 = new Employee { Name = "Yaniv", ID = "Cohen", Age = "31" }; Employee oEmployee3 = new Employee { Name = "Yoni", ID = "Biton", Age = "20" }; List<Employee> oList = new List<Employee>() { oEmployee1, oEmployee2, oEmployee3 }; System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string json = oSerializer.Serialize(oList); Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var employees = " + json + ";</script>"); //Then, to parse the list: jQuery.each(employees, function() { alert(this.Name); });