Link to home
Start Free TrialLog in
Avatar of KathrynGZ
KathrynGZ

asked on

Using JavaScript to call a VBScript OR converting a VBScript to JavaScript.

In an HTML document, I'm referencing a JavaScript file with the following code.

<script language="javascript" src="/ssi/js/lastrevdate.js"></script>

I have to use javascript, and I have to reference an external file (company intranet).

But so far, I have only been able to find this VBScript that does what I need:

Dim objFSO
Dim objFile
Dim dateModified

' Creates the object and assigns it to our variable.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

' Get access to the file
Set objFile = objFSO.GetFile(Server.MapPath("modified.asp"))

' Get last modified property
dateModified = objFile.DateLastModified

' You can format it as you like using the FormatDateTime command
This file was modified on the date and time below:<BR>
&nbsp;&nbsp;You can write out the date as is...<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%= dateModified %>;<BR>
&nbsp;&nbsp;or format it as you like!<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%= FormatDateTime(dateModified, 1) %>; at <%= FormatDateTime(dateModified, 3) %><BR>

' Kill our objects
Set objFile = Nothing
Set objFSO = Nothing

(Credit http://www.asp101.com/samples/modified.asp)

This is my question:

Can I somehow place this vbscript in the external js file so it will run? (I tried this but couldn't get it to work.)

OR

Can the vbscript be converted to javascript? If so, how?

Thanks in advance!
Kathryn
Avatar of James Rodgers
James Rodgers
Flag of Canada image

if what you are looking for is the last modified date of the current page try this

<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var modDate = new Date(document.lastModified)
  var modMonth = modDate.getMonth()+1
  var modYear = modDate.getYear()
  var modDay = modDate.getDate()
  if(modDay<10) modDay = "0" + modDay
  if(modMonth<10) modMonth= "0" + modMonth
  if(modYear<1000) modYear+=1900
       
  document.write("Page Updated: " + modMonth + "/" + modDay +  
                  "/" + modYear+"")
//-->
</SCRIPT>
Avatar of Zontar
Zontar

Well, that's a serverside ASP script, so you can convert it to JScript easily enough, but it's not going to run on the client, if that's what you're looking to do:

<%@LANGUAGE="JSCRIPT"%>
<%
var fso, file, dateModified;

fso = Server.createObject("Scripting.FileSystemObject");
file = fso.getFile( Server.mapPath("next.asp") );
dateModified = new Date(file.dateLastModified);

Response.write( dateModified.toLocaleString() );

file = null;
fso = null;
%>

What exactly are you trying to do?
Avatar of KathrynGZ

ASKER

Jester_48, thanks for the suggestion. I should have mentioned that I'm authoring on our company's web server, and I already tried the javascript lastModified document property.  For some reason it returns the current date and time. (For instance, every time I refresh the document in the browser, it displays the current date and time, regardless of when the document was actually saved.)

The VBScript in my initial post works when it's actually in the document.  But I'm constrained by the two factors mentioned above: 1) I need to reference an external script file rather than having the script right in the HTML file , and 2) I have to reference a javascript file.

Zontar, I put your script right in the document, and it worked fine there as well. But as soon as I put it in an external file, I couldn't get it to work. Since it was jscript, I tried it without the <%  %>, but I get "Error: Server is undefined." I'm still fairly new at scripting, so I'm probably missing something obvious. Can you tell me how to put your script in an external javascript file and have it work?

Thanks!

Kathryn
the reason you get the current date/time with the aswer i provided is that asp seems to work the same as coldfusion and the last modified date becomes the date/time the page was loaded, wasn't sure if that was the way with asp... but now i know

thanks..

what about some cahnges to zontars
function getDate(){
var fso, file, dateModified;

fso = Server.createObject("Scripting.FileSystemObject");
file = fso.getFile( Server.mapPath("next.asp") );
dateModified = new Date(file.dateLastModified);



file = null;
fso = null;

return dateModified.toLocaleString() ;
}

and called

as
<%=getDate()%>

Thanks, Jester_48. I tried your script in the external javascript file, and I still can't get it to work. When I used the following, I got a syntax error:

function getDate(){
var fso, file, dateModified;

fso = Server.createObject("Scripting.FileSystemObject");
file = fso.getFile( Server.mapPath("next.asp") );
dateModified = new Date(file.dateLastModified);

file = null;
fso = null;

return dateModified.toLocaleString() ;
}

<%=getDate()%>

When I tried changing the last line to

getDate()

I got a the server undefined error again.

Any ideas?

Thanks! I really appreciate your help (See why I set the point value so high? This seems like it should be simple, but everything I can think to try generates an error!)

Kathryn
ASKER CERTIFIED SOLUTION
Avatar of Zontar
Zontar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jon,

Thanks for your thorough answer! I checked with our webmaster and there's just one issue:  We're changing platforms in the near future, and at that point we're not supposed to use ASP any more. Anything we create now in ASP will have to be changed a few months down the road. (I asked him about PHP and he said that was out too.) Instead, we're supposed to use JSP (which I will need to learn!)

So I have two more questions:

1) Do you have knowledge of Java Server Pages to where you could adapt the code above?

2) Obviously I have a lot to learn about what works server side as opposed to client side. Do you know of a good online reference?

Thanks again for your time and expertise.

Kathryn
1. Sorry, I don't really do any Java or JSP, but the principles should be the same. (I only do PHP and ASP and a bit of Python, I'm afraid. Java is on my "Should really learn list" but I keep getting sidetracked.)

This looks like a good place to get started: http://www.jspin.com/

2. Um... not offhand. Interesting that this should come up, though, because I'm writing a book very largely about that subject right now.

Basically, anything that's not directly related to the browser itself or the page that's loaded in it is the province of serverside programming.
The saga continues...

Okay, this is embarassing. Jon, I tried your solution today and fully expected it to work--until I realized the filename is hard coded into the ASP file. The reason I'm using an external file is that I want to be able to call the js from multiple documents. My mistake for not making this explicit, plus the original VBScript I posted contained a hard-coded filename, so it is natural you would assume that's what I was looking for.

So . . . is there a way to pass the name of the file (preferably as a system variable) to your ASP page?

OR

I tried playing around with another solution today--I tried using document.write in an external js file to create my original VBScript in the page. E.g.

document.write("<%Dim objFSO");
document.write("Dim objFile");
document.write("Dim dateModified");

etc.

The problem I ran into here is that I can't get line breaks between the lines! I tried document.writeln. I tried assigning \r\n to a variable and document.writing that between lines. I tried %0A%0D or something like that (copied it from someone's web page). Nothing would make the lines break at the right places, so what the browser got was

Dim objFSODim objFileDim dateModified

which of course it couldn't make sense of.

The reason I'm leaning toward this solution is that it would only require me to insert one line of code in every document: the line that references the js file.

Thanks in advance for any suggestions you might have!

Kathryn
Since no further responses have been received, I've requested that this question be closed. Zontar, I can't use your solution because of constraints on our system; nevertheless, you went to a lot of work and the solution could work for others. So I've requested that the points be reduced to 250 and I will select your answer as the accepted one.

Thanks again!

Kathryn