Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Javascript StringBuilder for Beginners (ASP.NET 3.5 and above)

, 21 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Why not simply:markup = [];markup.push('');markup.push('');markup.push('');markup.push('');markup.push('Sample \'StringBuilder\' HTML');markup.push('');markup.push('');alert(markup.join('\n\r'));markup.toString() could...
Why not simply:
 
markup = [];
 
markup.push('<html>');
markup.push('<head>');
markup.push('</head>');
markup.push('<body>');
markup.push('<h1>Sample \'StringBuilder\' HTML</h1>');
markup.push('</body>');
markup.push('</html>');
 
alert(markup.join('\n\r'));
 
markup.toString() could also be used if there is no need to add any separator. Uses native Javascript with no need for AJAX, or ASP.NET. Should therefore work cross-platform and cross-browser.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

GladToBeGrey

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralRe: To follow the culinary model, why use a 'Death Star' to crac... PinmemberGladToBeGrey23-Apr-11 22:39 
GeneralRe: I think I already answered your question. Yours is an altern... PinmemberAlbin Abel23-Apr-11 7:12 
I think I already answered your question. Yours is an alternate solution, but does your question sounds like one should not use asp.net's string builder?, then I completely disagree. It is just a usability choice to get a plum cake though you point to a chocolate cake. The main idea of Asp.net ajax framework is bring the server side controls accessible at client side as well and use the same syntax style. If one knows c# then they can follow the same style at javascript. Such a novel idea is expressed in string builder class. My tip already having the header saying it is for asp.net users so string builder syntax is more familiar than native javascripts.
 
As you said "If OOP purity is your requirement, then create a stringBuilder class based on the above". sys,StringBuilder doing the same plus in a c#/ .Net friendly way. Did you read the last line of my tip? Sometimes I can eat whatever available ready than I myself become the baker man.
 
What is the problem you think depend on asp.net in a asp.net page? At client side everything rendered as html and native javascript (specifically oops oriented). Somebody tell you asp.net independent solution, but all their aspx pages has 100s of server controls. Is that means only javascript should be independent?!. In contrast it is friendly than do native javascript for a .Net developer.
 
Let us take a few examples...
var sb = new Sys.StringBuilder();
sb.append("Hello World ");
sb.append(String.format("You are now {0} years old", 5));
sb.append(String.format("I got {0:c} ", 50));
sb.append(String.format("I got dollors {0:n} ", 1200));
This style is not unfamiliar to a .Net programmer. But how you cook it in native javascript? May be you know elegant ways. But not a .Net developer.
 
So consider your tip as alternative. Ofcourse your tip has advantages. But don't keep on knocking "Why not simply". Simply not means something what the computer easily understand, but what people easily understand. Ajax library is one of the novel approach for .Net developers to do things in the .Net way.
 
Why somebody create a jQuery framework when everything possible by a native javascript. Same way stringbuilder is a framework class. My idea is just expose it to the beginner .Net developers. What is wrong in it. Why they must use native javascript?
 

GeneralReason for my vote of 5 Good solution. PinmemberPrasanta_Prince22-Apr-11 4:26 
GeneralReason for my vote of 5 Practical alternative with some good... PinprotectorAspDotNetDev21-Apr-11 11:51 
GeneralTo answer your question "why not", the implementation of the... PinprotectorAspDotNetDev21-Apr-11 11:50 
GeneralThe question "Why not" is not relevant. Your method has adva... PinmemberAlbin Abel21-Apr-11 8:19 
GeneralRe: No, my question is why create a dependency on ASP and AJAX a... PinmemberGladToBeGrey22-Apr-11 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 21 Apr 2011
Article Copyright 2011 by GladToBeGrey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid