0

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!

iank
  • 800
  • 3
  • 10
  • 32

4 Answers4

3

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.

Community
  • 1
  • 1
AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
2

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.

  • Files ending in .asp are ASP (vbscript) files.
  • Files ending in .aspx are ASP.NET files.
  • Files ending in .jsp are JSP files.
Dan Ellis
  • 5,537
  • 8
  • 47
  • 73
  • Not true. http://underscorejs.org/#template https://github.com/visionmedia/ejs You can also put in raw Javascript (assuming you're using Node or underscore.js templates) inside of <% %> tags, and it will be ran before the page/ html element is displayed. – AlbertEngelB Sep 20 '12 at 23:52
1

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Just curious, but how are you certain it's JSP and not possibly, say, ASP? – Colin Brock Jun 22 '12 at 16:11
  • @Colin: PHP doesn't use `<% %>` syntax and I wasn't aware that ASP uses same syntax as well. – BalusC Jun 22 '12 at 16:14
  • I erroneously included PHP in my original comment (edited after realizing my mistake), but was just curious since your answer seems very confident it's JSP. Again, just didn't know if I was missing something! – Colin Brock Jun 22 '12 at 16:16
  • @Colin: at least I said that it is "recognizeable" as JSP, not that it is "definitely" JSP. Who knows what kinds of other possibly obscure view/templating technologies exist :) Anyway, I'll probably just delete this answer once the OP confirms what view technology s/he is actually using. – BalusC Jun 22 '12 at 16:18
0

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 %>;
jbabey
  • 45,965
  • 12
  • 71
  • 94