Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-03
11
Medium Priority
?
698 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:KathrynGZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
11 Comments
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9675194
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>
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9675268
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?
0
 

Author Comment

by:KathrynGZ
ID: 9680669
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Expert Comment

by:James Rodgers
ID: 9680930
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()%>

0
 

Author Comment

by:KathrynGZ
ID: 9681160
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
0
 
LVL 11

Accepted Solution

by:
Zontar earned 1000 total points
ID: 9683991
You can't use serverside objects in a clientside script.

Here's what *can* you do. Create an ASP file that generates the clientside JavaScript.

1. Save the following into an ASP page named "last-mod-js.asp":

<%@LANGUAGE="VBSCRIPT"%>
<%
Dim objFSO
Dim objFile
Dim dateModified
Dim strFileName
Dim strLastModified

' Put the name of the desired file here:
strFileName = "empty-cell.html"

' 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(strFileName) )

' Get last modified property
dateModified = objFile.DateLastModified

' Put together the last-update message string
strLastModified = "The file <b>" & strFileName & "</b> was last updated at <b>" &_
  FormatDateTime(dateModified, 3) & "</b> on <b>" & FormatDateTime(dateModified, 1) & "</b>."

' Set the Content-Type to "text/javascript" so the browser knows how to handle this content
' If this isn't done, it will cause an error in the browser
Response.ContentType = "text/javascript"

' Now write a JavaScript variable declaration
Response.Write "var dateLastMod = """ & strLastModified & """;"

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

2. Now you can call this ASP file as though it were a JS source file.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>

<style type="text/css">
  p {font-size:12px;}
</style>

<script type="text/javascript" src="last-mod-js.asp"></script>
<script type="text/javascript">

//  the lastLastMod variable was defined in last-mod-js.asp; we can use it in JavaScript
//  anywhere in the page after that's been done

window.onload = function (){  document.getElementById("lastModMsg").innerHTML = dateLastMod; };
</script>

</head>
<body>

<p id="lastModMsg">Please enabled JavaScript in your browser.</p>

</body>
</html>

See it in action here:

http://www.shorthand.org/jon/test/js-with-asp/last-mod.html

Notes:

1. You can't get info about a file that's not loaded in the browser using clientside JS alone. This MUST be done using serverside scripting.

2. I've pulled the same trick using PHP as well. I imagine it can be done with just about any serverside programming language.

3. Notice that there's no need for a <noscript> tag. If the user doesn't have JS enabled, they see a message asking them to enable it. If they do have JS enabled, that message gets replaced by the "Last Updated..." message.

Any questions? Just holler.

cheers

jon.
0
 

Author Comment

by:KathrynGZ
ID: 9695643
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
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9696619
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.
0
 

Author Comment

by:KathrynGZ
ID: 9719554
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
0
 

Author Comment

by:KathrynGZ
ID: 9821856
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question