views:

184

answers:

8

Hello! I have another similar question to the one I just asked. I'm currently diving into web development after ten years of desktop development and I'm trying to get a high level grasp on the many concepts I'm learning. Two recent concepts I've been reading up on are Javascript and ASP.NET. I understand javascript is client-side script while ASP.NET is server-side script.

  1. Can they be used for the same purpose? Or do they serve wildly different purposes?
  2. How are the two used together?
  3. Where does javascript fit into the MVC pattern? M, V, or C?

I apologize if these are strange questions and I'm comparing apples to oranges, forgive me as I'm still a huge gigantic noob :)

Thanks in advance for all your help!

+1  A: 

They can go together hand in hand, you can have client-side validation to minimize http requests but as a fall-back rely on server-side validation; another example would be using Javascript to dynamically make an http request which retrieves server-side code so you can do your little "Ajax" action on your web page

AFAIK Javascript isn't a model, view or controller - it's an entirely different component altogether. Models retrieve data, Views utilize the models and render the output, and controllers bind models and views together.

Note: Javascript can be used for server-side scripting entirely... since ECMAScript is a fully fledged programming language but 99% of the time it's used for client-side.

meder
If anything though, js would fit into the view, of those 3
David Archer
IN the ASP.NET MVC project I think javascript is part of the view.
CD
+1  A: 

Hi Beeph.

  1. They can be used for many of the same purposes. You already know that ASP is server-side and javascript client side, so you answered this already.
  2. One common way they are used "together" is AJAX. See another answer of mine for a simple explanation.
  3. Great question. It's completely separate from MVC.

MVC is typically what would be done server side to separate responsibilities. On the client side there are three layers as well - Behavior (javascript), Style (CSS), and Content (HTML). The idea is that you have your HTML be a fully functioning version of your site, and you add additional behaviors to it with javascript. Your Style, instead of being inline style="" attributes, is done via CSS.

Some common examples of javascript include:

  • Client-side validation (email validity, password strength, password matching)
  • Sorting (see TableKit)
  • Animations/Effects - Fades, Slides, Accordions
hobodave
+3  A: 

Can they be used for the same purpose? Or do they serve wildly different purposes?

It depends. Sometimes they can, but in general they are slightly different.

How are the two used together?

Usually, server side is responsible for application business logic, persisting data, security and similar tasks. Javascript on the other hand mostly is used to achieve desktop-like web applications (through asynchronous calls to server side) and better user interface.

Where does javascript fit into the MVC pattern? M, V, or C?

It's in View part.

Arnis L.
+2  A: 

Some controls in ASP.NET Web Forms rely heavily on JavaScript for their functionality - LinkButton for postbacks, and also DropDownList and CheckBox for postback (when AutoPostback is enabled).

Edo
A: 

Typically (and I say "typically" because there are no hard-and-fast rules in web development), JavaScript is part of the "view" and included with the HTML and CSS code that the browser renders into a web page. JavaScript does the little animation effects, responds to browser events, and you can use it to overcome many inconsistencies between browser versions. JavaScript is also able to send requests (XMLHttpRequest, a.k.a. AJAX) to the server and retrieve data without performing a full page refresh. It then uses this data to modify the web page (represented in the browser as a tree according to the Document Object Model spec), through a set of functions provided by the DOM API.

ASP.NET is a framework for server-side processing. It's mainly used for the "model" (database interface) and "controller" (business logic). I am not particularly fluent with ASP.NET, but suffice to say it provides a very rich set of libraries for working with data and data stores, dynamically building up the web page representation by stitching together data with pre-written page templates. ASP.NET isn't tied to a particular programming language; rather, you can use any language that is available for the .NET Framework.

As for MVC, I am a bit confused on that point as it relates to the web. From what I understand, the Model, View, and Controller are a way to segment the server-side application, but there is also some mixing of concerns in the browser. For example, you can use JavaScript to validate user input, but this validation must be repeated on the server because the user can circumvent the JavaScript. Similarly, the server generates a view and sends it to the browser, but this view can be heavily modified by scripting in the browser.

James M.
+5  A: 

ASP.NET works on the server and prepares a bunch of (X)HTML that is being send to the browser. Internally it uses an MVC architecture. The end result though is just HTML.

Javascript comes into play once the HTML output of ASP.NET reached the browser. Traditionally, for anything to change on the page once the ASP.NET output is complete, the page needs to be refreshed completely, a complete new request made to the server, which answers with new HTML.

Javascript enables the programmer to change things in the browser dynamically "after the fact" in what would otherwise be static output by ASP.NET.

Therefore the two aren't really related at all, certainly Javascript lives outside ASP.NET's MVC architecture. The two can work together in the sense that Javascript works on the HTML that ASP.NET output, can be used to fetch data from the server being served by ASP.NET and ASP.NET outputting Javascript code together with HTML. The two are never directly interacting though, one is outputting strings on the server and the other is reading and modifying them on the client, there's nothing ASP.NET specific about it.

The output side (HTML + Javascript code generation) may be tightly integrated in the .NET framework, under the hood you're actually looking at several technologies though:

  • ASP.NET, which outputs
  • HTML, which the browser interprets into a
  • DOM, which can be dynamically altered using
  • Javascript, which can fetch data from the server via
  • [ goto ASP.NET ]
deceze
A: 

They're not related at all. One is server-side, the other is client side.

MartinHN
http://en.wikipedia.org/wiki/Server-side_JavaScript
David Dorward
Yet another Leaky Abstraction: http://www.joelonsoftware.com/articles/LeakyAbstractions.html
MartinHN
+1  A: 

I see that you have got answers for your questions, but I hope if you could read that article

Using JavaScript Along with ASP.NET

Summary: Learn how to apply JavaScript in ASP.NET applications. Also learn ways of doing control focus, button rollovers, alerts and placing text in the browser's status bar.

Ahmed
great link, thank you!
BeachRunnerJoe