I'm maintaining a javascript page and I'm just wondering what surrounding a variable with this will do?
<%= variable %>
I tried searching but wasn't able to find anything relevant, so I'm sorry if this has been answered already! =)
Thanks!
I'm maintaining a javascript page and I'm just wondering what surrounding a variable with this will do?
<%= variable %>
I tried searching but wasn't able to find anything relevant, so I'm sorry if this has been answered already! =)
Thanks!
These are ERB-style tags. They're used in a variety of languages and formats, generally they are used to input simple variables into the page or do some logic on the variables passed to the page.
Seeing as it's a Javascript page, you might want to check out the documentation for underscore.js's templating engine, it will give you a good idea what's going on. http://underscorejs.org/#template
Another SO page that explains the differences between <%- %>, <%= %>, and <% %> tags in Underscore: Boolean checks in underscore templates
Interpolate will put in the element as text (not HTML), escape will allow you to embed HTML, and evaluate will run what's inside that as JS.
This could be a number of different server side languages including JSP, ASP (vbscript) or ASP.NET, but it's not JavaScript. You'll be able to tell by the file extension of the file that you're editing.
.asp
are ASP (vbscript) files. .aspx
are ASP.NET files. .jsp
are JSP files.This is recognizeable as a JSP scriptlet expression. You see this usually in files having .jsp
extension. JSP is a Java based server side view technology, similar to PHP and ASP. This is totally unrelated to JavaScript. JSP (and PHP and ASP) are merely HTML code generators. They runs on the webserver and produces HTML. JavaScript is part of that HTML and runs on webbrowser only.
The particular piece of code basically prints the String
representation of the value hold by the variable
to the HTTP response at exactly that point where the expression is been declared. It's like a System.out.println(variable)
, but then with the HTTP response body as output. A JSP/Servlet container can be configured to execute them for .js
files served by the servletcontainer as well, but this is thus not the default configuration.
JSP scriptlet expressions are by the way a rather old school way of writing JSPs which leads to tight coupled and unmaintainable code. See also How to avoid Java code in JSP files?
Most likely you are looking at ASP tags, used by ASP and ASP.NET in the client-side code to communicate with the server.
For example, assume I have this hidden input, nested in some repeater control, on my page:
<input type="hidden" name="foo" id="foo" runat="server" value="blah" />
In order to get the rendered ID (which will not just be "foo" if it's nested in an ASP control), i need to grab it from the server:
var hiddenID = <%= foo.ClientID %>;