2

The new MVC template which came with VS 2012 contains the line of JavaScript below in "_LoginPartial.cshtml". I have a question about this JavaScript syntax. Is this JavaScript or Asp.Net specific syntax? Can we use JavaScript calls in normal HTML?

<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
Nexus23
  • 6,195
  • 9
  • 50
  • 67

2 Answers2

1

You can use <a href="javascript:foo();">..</a> in normal html markup - it's not Asp.Net specific - but I don't think it's regarded as very good unobtrusive js. See the discussion here: How to call javascript function instead of href in HTML where the accepted answer from @Dutchie432 includes this better form:

<a href="javascript:void(0);" onclick="foo(args);">

There is also some more discussion of alternatives here: http://www.pageresource.com/jscript/jlinktut.htm

Community
  • 1
  • 1
Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
1

I see this is a Log off link, and the JS seems to be submitting the form. Surley you can just have an action in your controller that deals with logging off ? that would be a better design.

As mentioned before this format of JS is not unique to the .net template you are using , it can be applied to plain HTML.

Waqar
  • 758
  • 4
  • 15