Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts

Tuesday, 16 April 2013

ASP: pass a variable to JavaScript

   


One of the most often asked question in forums is "how do I pass an ASP variable to JavaScript?"
It is quite common to find out that people ask the question the other way round: "How do I pass a JavaScript variable to ASP?"
Ok, the two things are completely different and they involve completely different solutions.

First of all, let me explain something you might already know. ASP (or to be precise VBScript) is a server side language, while JavaScript is a client side language. The two work on different levels: ASP is executed before the page is completely loaded, while JavaScript works only after that. For that reason we cannot pass a JavaScript variable to ASP without reloading the page.

Tuesday, 19 March 2013

ASP: conditional statements with If...Then...Else...ElseIf

   


In ASP we can use conditional statements in different ways. The basic syntax we are used to, is If ... Then ... Else ... End, however we will see that there are some interesting things about it.

First of all let's consider a simple conditional statement in ASP. As you already know, we do not use any brackets or parenthesis. A basic example could be:
<%
Dim numVar
numVar = 5
If numVar = 5 then
  response.write("The variable numVar is 5")
End if
%>

Tuesday, 19 February 2013

ASP: CDate

   


CDate is an ASP function that converts a valid date and time string to a Date Type value and returns it.

Because of the above we need to use another ASP function in order to be sure to convert a valid date. The function's IsDate.

In the following short post, we are going to see how to use both functions and create a short script that converts a date.

Thursday, 17 January 2013

ASP: special characters

   


ASP

One of things that newbies find difficult when trying to learn ASP is the way code is tructured and what all those special characters are there for.



In this short post we are going to see the most common special characters we can find in an ASP page:
  • &
  • '
  • _
  • :
  • .
  • <% ... %>
  • <%= ... %>
As you will see, some of the above might hide even little secrets and surprises.

Tuesday, 15 January 2013

ASP: variables naming convention

   


When naming variables in ASP, we should consider some conventions.
ASP

Because the way we write code is quite important for us and, possibly, other people, we do need to keep things as much readable as we can. That means, for example, we should use indents when writing procedures and functions or we should follow a common structure in code blocks.

In general we need to make things easy to read and understand.
That is why there are common naming convention for variables. And in this article we are going to explore them.

Thursday, 13 December 2012

ASP: permanent redirect with HTTP 301 status code

   


There are situations when we need to redirect the visitor to a web site to another place.
This is done for example when the web site is no longer updated or valid.
In order to do so we can redirect the visitor permanently with HTTP 301 response code. That is done because we need to tell the user-agent that the location is permanently moved elsewhere.
I believe you all know what HTTP headers are... or maybe we should start from there...

Tuesday, 23 October 2012

ASP: CursorType and LockType

   


With ASP we usually connect and retrieve data from a database. We do it almost every day and we use the relative code without really thinking about it. Things are working and we get what we need, however... do we really know what we are doing?
I’m talking about two particular properties: CursorType and LockType.
In my experience, when I first used ASP to open a recordset, I used the two properties without really understanding their significance. But, because I am basically a curious guy, I wanted to understand what those numbers are, why I needed to use them and why I got different results, when changing them.
Let’s see together what are those two properties.

Tuesday, 2 October 2012

ASP: the InStr function

   


One of the most used functions in ASP is InStr.
In this short article we are going to see how to use it.

The Syntax
The InStr function has the following syntax:
InStr([startOfStr,]stringSearched,search[,compare])
where:
startOfStr is optional and it specifies where the search will begin. By default is set to1 (first character of the string). It is required when compare is used.

Thursday, 27 September 2012

ASP: how to get a file extension

   


While we have already seen how to upload files and how to work with the Scripting.FileSystemObject, today we are going to see how to get a file extension, for example before authorising a user to upload a file to our server. We all know very well how important is security when working with the file system objects: for example, we don't want the user to upload executable, or other potentially malicious files on our server.
Said that, we can create a short ASP function to check the file extension, and grant or deny the upload.
Let's see how...

Tuesday, 25 September 2012

ASP: how to keep track of downloaded files

   


Today we are going to see how we can keep track of downloaded files in a web site.
We normally link files with an hyperlink. The trick in this case is to pass the file information to another ASP page and then - after "taking note" of the relevant information - initiate the download.
Ready to see the code? Follow me...

Thursday, 5 July 2012

ASP: how to use datediff

   


As I said in my last post, today we are going to explore a VBScript function called DateDiff. There's not much mystery behind it, in fact, as the name says, DateDiff calculates the difference between two dates. The resulting value can be in different format and, generally speaking, the function can be used in many situations.
let's see how it works.

Tuesday, 3 July 2012

ASP: how to use dateadd

   


ASP is a powerful tool. Here, as usual, we are talking about a VBScript function that let us manipulate dates: DateAdd.
Whenever we need to perform operations on a date, we can benefit from the use of the this function. In the following post we are going to see how to use it.

Tuesday, 26 June 2012

CSS & ASP: let the user choose the style

   


When we create a web site, we usually decide the look of it and the way the visitor is actually experiencing it. Sometimes we might need to create different look for the same site, especially if we are dealing with specific groups of users. I'm going to explain in a simple way how to let the user choose the style of the web page according to its needs. To do so, we are going to use CSS and ASP.

Monday, 21 May 2012

ASP & jQuery: linked list boxes with AJAX

   


Today, I would like to share a quick and easy solution to link 2 list boxes with jQuery and AJAX.
Situation: we have two list boxes. One (number) is already populated. The second (letter) is populated according to what is selected on the first list box, via AJAX (not reloading the page).
We need to build two ASP pages.
Let's start and have fun!

Friday, 4 May 2012

ASP: avoid SQL injections

   


If you work a lot with ASP and SQL, you might already know about SQL injections.
It's quite clear that every time we offer a visitor the possibility of filling a form (whatever its purpose may be), there's a security risk for our database. Basically, an attacker can insert some code in a form element, which will serve as a breach, allowing access to data stored in the database.
How that's done is not the main topic of this short post, however we should be aware of the fact that those threats are often used to update, delete and insert data, or in worst cases, they are used in order to gain access to reserved areas of a web site.
In this article, we are going to create a small VBScript function to avoid SQL injections.

Monday, 23 April 2012

VBScript: how to schedule the opening of an ASP page

   


Following an exchange of ideas with Still_ASP, a loyal reader of the web thought, I thought to write something about scheduling events on a Windows server.
Sometimes we might need to trigger some events independently from the actual opening of a page or a specific action taken by a user. If nobody is browsing your page, how could we trigger an event anyway?

To do so, we need to use a little trick that I will shortly explain.

Monday, 16 April 2012

jQuery, JavaScript & ASP: barcodes resources

   


Following a brief exchange of thoughts with Carlo (one of the web thought readers), I have decided to publish a list of resources aimed to the creation of Barcodes. Originally, Carlo wanted to insert a barcode in a PDF document using FPDF, however the subroutine he found generates a HTML table that is almost impossible to embed in a PDF with FPDF.
Other solution are possible. The point is to create a Barcode image, save it on the server, and then insert it into the PDF. I haven't explored the above idea, but I think that the following resources might help some to accomplish the task.
Here's the list:

Barcode Coder (jQuery)

jQuery-Barcode (jQuery)

Pure ASP Barcode Generator (pure ASP)

ASP Barcode Script (pure ASP - Thanks to Carlo)

Barcode Generator (JavaScript)

Creating a Code 39 Barcode using HTML, CSS and Javascript (JavaScript)

Javascript Code128 Barcode (JavaScript)

Wednesday, 11 April 2012

ASP & CSS: dynamic style rules for database items

   


If you work a lot with ASP and get data from databases, you might have been in need of creating different CSS rules for each item in a recordset.
We can create dynamic CSS rules with a simple loop, assuming we know the number of items to which we will apply the rule. The point is that we don't know how many recordset items we will get from the database, but we can create rules according to different situation, as I will explain shortly.

Now follow me and see what we can do.

Friday, 6 April 2012

ASP: the dictonary object

   


I've recently worked a lot on ASP arrays. One thing that is rarely used in ASP is the dictionary object which is similar to an array but has some useful methods and properties, making it easier to manage.

The dictionary object can store information in pairs: key and item. Every key refers to an item. When we build a dictionary object we create a relation between a key and an item, so we can recall an item by its key.

An example
To create a dictionary object we simply do the following:
<%
Dim dicObj
set dicObj = Server.CreateObject("Scripting.Dictionary")
Now our object is ready to accept keys and items. In order to add them we use the .add method:
dicObj.add "one","firstItem"
dicObj.add "two","secondItem"
and so on.

Wednesday, 28 March 2012

ASP: calculate if a number is odd or even

   


In this short post I will explain something that I see asked often on forums and ASP help site.
How can we calculate if a number is even or odd?

The question is quite easy and the answer is hidden in the MOD function. I've talked about that function in a past article (ASP: resulting recordset in two columns), where we used the MOD function in order to create a two columns table and display data using the same table.

As a reminder, "the MOD function provides the remainder value in a division. The remainder is what's left over after you do a division."
Apart from the fact that I'm starting to quote myself - and I probably should talk to my wife who is a psychotherapist, it is quite easy, even if you are not a math expert, to understand how we can benefit from the MOD function to determine if a number is odd or even.