Converting VBScript array to Java Script Array in classic ASP - javascript

I have a classic ASP page which is written in VB script, and there is a script tag which Java Script is used as below.
I am trying to cast VB Script Array(codeList) to Java Script Array(availableCode) with for loop but the first row of VB Script Array keeps being casted in every row of Java Script Array.
$(document).ready( function(){
var availableCode = new Array();
for (idx=0; idx < 3; idx++)
{
availableCode[idx]=<%=codeList(idx)%>;
alert (idx);
alert (<%=codeList(idx)%>);
alert (availableCode[idx]);
}
});
I tried the below as well but getting a syntax error.
availableCode[idx]=<%=codeList(%> idx <%)%>;
Could anyone advise how to cast expected values? Thank you.

I think you need to do it the other way round ie create your index (idx) within VBScript, then write it to the Javascript:
$(document).ready( function(){
var availableCode = new Array();
<%
dim idx
for idx = 0 to UBound(codeList)
%>
availableCode[<%=idx %>] = <%= codeList(idx) %>;
alert (<%= idx %>);
alert (<%= codeList(idx) %>);
alert (availableCode[<%= idx %>]);
<% next %>
});
if the elements of codeList are string values, you will need to quote to prevent javascript errors
$(document).ready( function(){
var availableCode = new Array();
<%
dim idx
for idx = 0 to UBound(codeList)
%>
availableCode[<%=idx %>] = '<%= codeList(idx) %>'; // quote here
alert (<%=idx %>);
alert ('<%= codeList(idx) %>'); // quote here
alert (availableCode[<%= idx %>]);
<% next %>
});
The issue you were having I believe was that idx did not exist on the server-side (VBScript) and therefore when used in this statement <%= codeList(idx) %>, idx was being converted implicitly to 0. Thus always returning the value in codeList(0).

You can use Join() to output your array: this will also make your client-side code more compact.
$(document).ready( function(){
var availableCode = ['<%=Join(codeList, "','")%>'];

Related

how do I use for loop inside script tag in ejs

I try to draw chart, using chart.js.
To get my data, I try to use ejs tags
for example,
in ejs I input html like this, and it works well.
<p>date: <%= today %></p>
<p>temperature: <%= data[data.length-1].temperature %>℃</p>
<h1>5days average temperature</h1>
<ul>
<% for(var i = 0; i < 5; i++) {(function (j) {%>
<li><%= dateList[j] %>%></li>
<ul><li><%= avgLocTmpList[i] %></li></ul>
<% })(i);} %>
</ul>
but when I use chart.js, I have to use ejs tags like this inside .
<script>
var temper0 = '<%= data[0].temperature%>';
tempList.push(temper0);
var temper1 = '<%= data[1].temperature%>';
tempList.push(temper1);
var temper2 = '<%= data[2].temperature%>';
tempList.push(temper2);
</script>
I can get data[0].temperature as well inside tag.
but, I hope to use for loop inside .
such as
<% for(var i = 0; i < 5; i++) {
<%= data[i].temperature %>
<% }%>
but I can't handle this.
Is there any option or way to handle this for loop inside ??
Thanks, in advance.
In a similar situation i used this inside script tags and worked!
So try the following and check if it works.
<script type="text/javascript">
<% data.forEach(function (d) { %>
console.log("<%= d %>"); // or anything you want to do.
<% }) %>
</script>
Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.
<% for(var i = 0; i < 5; i++) {
<%= data[i].temperature %>
<% }%>
Use this: <% for(var i = 0; i < 5; i++) { %>
I was trying to get property images from my db this way:
<script>
let data= JSON.parse('<%-JSON.stringify(property)%>')
let dataArray= [];
for (var i = 0; i < data.length; i++) {
let image = data[i].image_name
dataArray.push({
id: i, src: '/uploads/properties/' + image
})
}
</script>
I have a MongoDB database and I wanted to sum up all the prices of items that the customer added to his shopping cart I needed the answer that you asked so I tried the above examples even if they work fine it gives red underlines to the codes. So you must quote the code if you are going to use the variables that come from the server side. This way you don't get any red underlined code and it looks perfectly fine.
<script>
let prices = []
let pricex;
let sum = 0;
" <% for (let i = 0; i < products.userCart.length; i++) { %>"
pricex = "<%= (products.userCart[i].quantity *
products.userCart[i].price) %>";
prices.push(pricex);
" <% } %>"
for (let i = 0; i < prices.length; i++) {
sum = sum + parseInt(prices[i]);
}
console.log(sum);
</script>

how to invoke a variable in javascript with concatenation of two strings

in my .aspx page I've got something like this- I create dynamically javascript variables which look like this
var region11 = ''
var region12 = ''
So when I click on a region of svg I get the id of the region which is for example 11 and I should set the href property region11 variable.
Unfortunately the javascript debbuger says that it cannot find region variable
<% var regions = ICYGEN.MRF.Data.CityData.SelectProvinces(); %>
<% foreach (ICYGEN.MRF.Data.Entities.Region region in regions)
{ %>
var region<%= region.RegionID %> = '<%= MRFUrlHelper.Elections2015_RegionUrl(region.RegionID,region.Title, 1026) %>';
<% } %>
jQuery(window).load(function () {
var svgMap = jQuery('#svg-map').contents().get(0);
alert(region11);//it's Ok here and it finds it
jQuery('svg path', svgMap).click(function () {
var ids = String(this.id);
location.href = region+ids; //can not find
You're using region instead of region11 in your click event handler:
location.href = region+ids; //can not find
Change to:
location.href = region11+ids; //can not find
Or I suppose:
location.href = region<%= region.RegionID %>+ids; //can not find

Assign C# string to Javascript variable

var nUserID = '<% if (ids != null) {ids.userID.ToString();}%>'
Note: ids is not null nor ids.userID is null but em getting nUserID = ""
Is there any way to convert C# string to Javascript string?
For write it direct on page you need to use the Response.Write as
var nUserID = <% if (ids != null) {Response.Write(ids.userID.ToString());}%>
beside that, if you need to add at the end ; or place it inside quotas is up to you.
The <% %> did not write on page, just run the code.
The <%= %> write on the page as the Response.Write
var nUserID = '<%=((ids != null) ? ids.userID.ToString() : "0")%>';
Use Something Like this:
<script type="text/javascript">
var nUserID = '<%=ids.userID.ToString()%>';
if(!nUserID)
{
//check for null in javascript
alert(nUserID);
}
</script>
More appropriate solution would be to to save your ids.userID to data attribute of some related html element or just create invisible div element with all C# data you want to use in your javascript code as data attributes. And then use jQuery to extract that data in scripts.
<div id="settings" style="display: none;" data-user-id="<%= ids.userID %>"></div>
and javascript:
var nUserID = $('#settings').data('user-id');

Change asp variable in Javascript?

I'm editing a previous coworkers code so I don't know 100% how it works. It is written in Javascript and ASP. Here is the current code
<script>
var sessionDone = 0;
<% if session(ReturnSessionID()) = 1 then %>
sessionDone = 1;
<% endif %>
</script>
and then there is some html with links, like this:
<a href='#'>link</a>
What I want is this:
<script>
var sessionDone = 0;
var counter = 0;
$('a').click( function() {
sessionDone = 1;
if (counter == 3) {
<% session(ReturnSessionID()) = 1 %>
} else {
counter += 1;
}
});
</script>
When I do this, the line
<% session(ReturnSessionID()) = 1 %>
automatically gets run even if the if statement isn't true and even if I did not click a link. How do I make the link only get executed when counter is 3?
Note: I do not know ASP and I don't really need to learn it, this is the only time I will be using it.
ASP is being executed on the server. The resulting HTML/JavaScript is being sent to the browser and then there the JavaScript is being executed. You cannot mix it like this.
When you want client-side JS to update a server-side variable, you have to make a request from the client to the server, which tells the server to update the variable.

Is there a way to use a Ruby loop inside of HAML's :javascript region?

Inside of HAML, can we have a loop inside the :javascript region?
This will work:
- 10.upto(20) do |i|
:javascript
document.getElementById('aDiv').innerHTML += '#{i}';
and this will not:
:javascript
- 10.upto(20) do |i|
document.getElementById('aDiv').innerHTML += '#{i}';
can the code above also be made to work as well?
%html
%head
:javascript
var foo = [];
#{
limit = rand(4)+3
array = (0..limit).to_a
array.map{ |i| "foo[#{i}] = #{rand(12)};" }.join ' '
}
console.log(foo.length);
%body
Running the above code gives this output:
<html>
<head>
<script type='text/javascript'>
//<![CDATA[
var foo = [];
foo[0] = 2; foo[1] = 0; foo[2] = 11; foo[3] = 8; foo[4] = 0; foo[5] = 1;
//]]>
</script>
<body></body>
</head>
</html>
As you can see, the big #{...} block (which may span multiple lines) runs arbitrary Ruby code. The result of the last expression (in this case the map{...}.join) is converted to a string and placed in the output.
Edit for Radek: If you want to declare a variable inside you Haml template, inside your JavaScript filter (which seems like an odd desire), then you need to be sure that the result of the block to_s doesn't produce unwanted output:
This Haml...
%p
:javascript
var foo = 12;
#{x = 42}
var bar = #{x};
...produces this HTML:
<p>
<script type='text/javascript'>
//<![CDATA[
var foo = 12;
42
var bar = 42;
//]]>
</script>
</p>
Whereas this Haml...
%p
:javascript
var foo = 12;
#{x = 42; ""}
var bar = #{x};
...produces this HTML...
<p>
<script type='text/javascript'>
//<![CDATA[
var foo = 12;
var bar = 42;
//]]>
</script>
</p>
But before you do this, ask yourself: why am I creating complex Ruby variables in my view?
Shouldn't this variable have been declared by my controller?
this one works
%script
- 10.upto(20) do |i|
document.getElementById('aDiv').innerHTML += '#{i}';
Just wanted to add that the following gets you the type and CDATA, but without the funky behaviour of :javascript (I just had to implement something like this).
%script{ :type => 'text/javascript' }
\//<![CDATA[
- (10..20) do |i|
document.getElementById('aDiv').innerHTML += '#{i}';
\//]]>

Categories