2

I frequently write client side code in the code-behind section of my asp.net applications. I think its about time to get a refresher on how to write client side code on server side page.

I need to refresh mysel fwith the "double quotes" and "single quotes". Not sure what that is called, but can anyone point me to the correct resource where I can learn about the quotes and double quotes.

1 Answer 1

5

Writing javascript code, intended to be run on the client, on the server is to put it simply: bad, wrong, to be avoided.

You should prefer to write your javascript functions in separate js files and only invoke them from the server. The benefit of this is that you will no longer have to worry about single, double and triple quotes, your js will be cached, minified, gzipped, served from a CDN, optimized, ... The end result of mixing server side languages such as C#/VB.NET with javascript is just ugly and difficult to maintain code.

Now to the point. When you need to call some javascript function from the server and you need to pass it some arguments in order to not worry about quotes and stuff, I would recommend you to JSON serialize your server side object and pass it to the client function.

Let's take an example. Suppose that you have written a function which requires some arguments:

function foo(myModel) {
    alert(myModel.Prop1 + ' | ' + myModel.Prop2);
}

and now you want to invoke this function from the server, here's how to proceed:

var model = new
{
    Prop1 = @"some value that can contain ', \, "", anything you like to throw it in, etc..",
    Prop2 = "some other value"
};
var json = new JavaScriptSerializer().Serialize(model);
var script = string.Format("foo({0});", json);
ClientScript.RegisterStartupScript(GetType(), "foo", script, true);

Now you no longer need to worry about any quotes and escaping. No matter what value you throw in those properties, they will be correctly serialized when invoking the javascript function.

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

4 Comments

I would argue that "Writing javascript on the server is to put it simply: bad, wrong, to be avoided" is very, very incorrect, and I think most developers using Node.js would agree. That being said, this is a good way to go when using JS in .Net.
Yeah, I knew what you meant, but it never hurts to temper blanket statements like that, especially for programming ;) There's always an edge case!
Only time I've really used JavaScript on serverside is to script a message box or alert box with response.write.
@Darin Dimitrov... thanks for your reply. Lets say is Prop1 value is "javascript:openDoc('Weekly Expense.pdf','vbDocFile') how would I pass that from server side to a database string ?

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.