-4

I'm using some code I found online but want to understand it better. Works fine but I'm going to need to modify it. What does it mean that much of the js is enclosed in <% %> brackets? Code follows:

        <% AllNetLabs.App_Code.GeoHelper helper1 = new AllNetLabs.App_Code.GeoHelper();
            foreach (var item in helper1.GetSpatialData())
            { %>
                geometries.push('<%=item %>');
        <%  } %>
        <% AllNetLabs.App_Code.GeoHelper helper2 = new AllNetLabs.App_Code.GeoHelper();
            foreach (var item in helper2.GetAttributeData())
            { %>
                attributes.push('<%=item %>');
        <%  } %>   
  • Can you post a link to where you found that code? – rollstuhlfahrer Jan 18 '18 at 19:27
  • 3
    this looks .net and its a razor syntax means gets evaluated on server side – sumeet kumar Jan 18 '18 at 19:27
  • 3
    That doesn't look like JavaScript inside those brackets at all. Looks more like C#. You might want to double-check where you got this code, as well as what exactly you mean by "works fine". This would most certainly *not* work as JavaScript. – David Jan 18 '18 at 19:27
  • 1
    And frankly, the code is...not ideal. Using a loop to generate a bunch of `push` statements... – T.J. Crowder Jan 18 '18 at 19:30
  • Basically this either isn't JavaScript(likely) and if it IS JavaScript, it's templated by the library/script you're using, so we can't help you without further information. – zfrisch Jan 18 '18 at 19:34
  • OK. It is – user2084255 Jan 18 '18 at 20:22
  • Also, this is where I found the original reference related to OpenLayers: http://dev.openlayers.org/examples/vector-features-with-text.html – user2084255 Jan 18 '18 at 20:26

3 Answers3

0

It looks to me that the code in the <% %> blocks is a different language (C#?). Typically, this pattern is used to prepopulate javascript with some values server-side and I would guess that's what's happening here.

enkrates
  • 626
  • 1
  • 6
  • 17
0

Do you mean EJS? http://www.embeddedjs.com/ Example for the site:

<h1><%= title %></h1>
<ul>
<% for(var i=0; i<supplies.length; i++) {%>
    <li><%= supplies[i] %></li>
<% } %>
</ul>
-1

They're using dynamic data from the server to push into a JS array.

  • @FranVerona This *is* an answer to the question. If you don't think that it's a sufficiently *useful* answer to the question, you can reflect that opinion by downvoting it, but that doesn't mean it's not an answer. – Servy Jan 18 '18 at 22:07