PDA

View Full Version : Get the Last modified date of an external text file?



KegExpress
05-08-2006, 11:54 PM
Hi there all,

I'm new to JavaScript, so excuse me if this is in the "gosh that's so simple" area.

I was wondering if there was a simple JS function to get the last modified date of an external text file. I've tried the "document.lastmodified" function, but (obviously) this returns the current web pages' date.

Its just for a simple price list/data table page that will look for the newly uploaded "prices.txt" and use that as a "Prices correct as at: 08/05/06" type of thing.

all the best,

Tony L.

otaku
05-09-2006, 12:41 AM
document.lastmodified will work only with the current document. For external text file my suggestion is to use server side script instead.

Here is an example on how to do it using ASP.Net:


Dim strLastModified As String
strLastModified = System.IO.File.GetLastWriteTime(Server.MapPath("prices.txt")).ToString("D")
lblMsg.Text = "Prices correct as at: " & LastModified

djr33
05-09-2006, 01:05 AM
Otaku is right.

That's a server side thing... you can't do stuff on the server client side. Kinda the point, sadly.

However, i'd look into PHP before ASP. Your choice though.

KegExpress
05-09-2006, 01:24 AM
Ah Dang,

I wanted the "Quick-And-Dirty" answer, seems a bit OTT to have to learn yet another programing language :D

thanks of the help anyway,

Tony L.

PS: a QAD answer, after looking for a few days for it - now that's optomism!! lol

djr33
05-09-2006, 01:53 AM
The only chance is if the file stores that data in a place you can access it. Might work... dunno. :)

Twey
05-09-2006, 09:40 AM
I wanted the "Quick-And-Dirty" answer, seems a bit OTT to have to learn yet another programing languageFor just that one task, maybe, but a server-side language will serve you admirably in the future.

otaku
05-09-2006, 12:14 PM
However, i'd look into PHP before ASP. Your choice though.
I strongly recommend ASP.Net.
By the way djr33, ASP.Net and ASP are not the same thing. It's like comparing java and javascript.

otaku
05-09-2006, 01:44 PM
In fact we can do it using javascript.

Demo:
http://www.getelementbyid.com/demo/LastModified/

Simply place <span id="LastModified"></span> where you want the message to be displayed and change the path for: src="prices.txt"



<html>
<head>
<title>Last Modified</title>
<script type="text/javascript">
function getLastMod(){
var myFrm = document.getElementById('myIframe');
var lastModif = new Date(myFrm.contentWindow.document.lastModified);
document.getElementById('LastModified').innerHTML = "Prices correct as at: " + lastModif.toLocaleString();
}
</script>
</head>
<body>
<span id="LastModified"></span>

<iframe id="myIframe" onload="getLastMod()" src="prices.txt" style="display:none;">
</iframe>

</body>
</html>

mwinter
05-09-2006, 03:31 PM
In fact we can do it using javascript.Certainly. Two ways, at least. However, if this information needs to be displayed, your original suggestion of a server-side approach is far superior. Even if there's no necessity, using the server would still be preferable.


http://www.getelementbyid.com/demo/LastModified/That fails in Opera, at the very least.


var lastModif = new Date(myFrm.contentWindow.document.lastModified);The contentWindow property is a non-standard feature, and not all browsers implement it. Though it could be feature tested alongside the contentDocument property, one should also ensure that the document object is accessible at all:



var doc;

if ((doc = myFrm.contentDocument)
|| (myFrm.contentWindow && (doc = myFrm.contentWindow.document))) {
/* ... */
}
Mike

Twey
05-09-2006, 03:40 PM
I strongly recommend ASP.Net.Closed-source, ugly, platform- and webserver-dependant, and, in its most common usage, a variant of a language that was most certainly never designed for web programming (yes, I know ASP.NET can use other, more suitable languages, but the point is that most people don't, yourself included from what I've seen of your examples so far)? Oh, Mono is trying hard enough, but they haven't got anything even resembling a usable ASP.NET parser yet -- it can only parse C#, for a start. If you want a pre-compiled web backend language, use JSP, rather than Microsoft's poor attempt at a clone.

djr33
05-09-2006, 07:19 PM
By the way djr33, ASP.Net and ASP are not the same thing. It's like comparing java and javascript.Ok. I still don't like ASP stuff (and ms in general). But thanks for letting me know.

otaku
05-09-2006, 07:43 PM
your original suggestion of a server-side approach is far superior.

I think everybody including KegExpress knew that before your post don't you think ?
I will let KegExpress decide which solution is the best for him.



The contentWindow property is a non-standard feature

You're right, adding contentDocument allow the script to run with Opera and Safari. Thank you to point use in the good direction. The script as I sent without contentDocument will works perfectly on IE and moz/firefox/netscape6+



... that most people don't, yourself included from what I've seen of your examples so far...


I have no problem with VB.Net or C#. Both are suitable languages for me. Your above critique was a little negative and not really constructive for all the developers who choose VB.Net and they are a lot.

I learned ASP.Net, Java and PHP and my suggestion for someone who want to start OOP is ASP.Net because it's more suitable, easy to learn and understand quickly and completely free. You can also find a lot of free resources, tutorials and helping forums on the net.

You can create XHTML compliant pages very easily with ASP.Net. Web controls support rendering valid, well formed XHTML markup with a simple configuration option.

And once again the software to build your webpage and the database are completely free. Here is some useful links for someone who want to start learning ASP.Net quickly:

Visual Web Developer Express:
http://msdn.microsoft.com/vstudio/express/vwd/download/

SQL Server 2005 Express Edition:
http://msdn.microsoft.com/vstudio/express/sql/download/

ASP.Net 2.0 QuickStart Tutorial:
http://www.asp.net/quickStart/aspnet/

otaku
05-09-2006, 07:59 PM
Ok. I still don't like ASP stuff (and ms in general). But thanks for letting me know.

I am coming here since only 48 hours and it seems to be the philosophy here. Not everybody but It seems that there is some Web-Standard Ayatollah ;) who hate IE and Microsoft in general.

djr33
05-09-2006, 08:03 PM
IE has bugs. ASP and other things like it are proprietary and just simply not as good as PHP. If you want to use them, fine. But what's wrong with PHP? Open source, free and plenty functional.

Internet explorer is designed to not be compatible so it's "Better", causing many problems.

Applications like frontpage, word, etc. only broaden the IE/other browsers gap and make the net an annoying place.


How many times have you needed to correct a page so it can work in :gasp: both Mozilla and IE.
Mozilla is based on standards... IE is just a random mess created by microsoft. Sure, it's popular. But that doesn't mean compatible or even good.


Sorry if this is offensive to you. Feel free to do what you want. That's just my opinion.

KegExpress
05-09-2006, 08:28 PM
many thanks otaku,

this was just what I needed, I was worried that it was in the "that's so simple, I'm not even dignifying it with an answer" category - lol I have searched for a few days for what I needed - I was even looking for a simple java class to do the job - but to no avail.

as to the compatibility issue raised - well we are just a small outfit, and just about 95% of our customers use either IE or Firefox, so I don't see the issue ariising - tho in the future it no doubt will.

many thanks again all,

Tony L.
(a.k.a. KegExpress, BigChunks)

otaku
05-09-2006, 08:39 PM
How many times have you needed to correct a page so it can work in both Mozilla and IE.

Not that hard. Of course IE can be better but I remember making DHTML with Netscape 4. That was a nightmare believe me! I remember that some people hated NS4 and now it's IE turn.



Sorry if this is offensive to you.

There is no offense on my side djr33.



many thanks otaku

You are welcome Tony :)

Twey
05-09-2006, 08:47 PM
Not everybody but It seems that there is some Web-Standard Ayatollah who hate IE and Microsoft in general.That's true. In fact, just about all of us here will agree on this. I happen to hate Microsoft, believing their business practices and general philosophies reflect far too closely the Japanese proverb, "bushinisu wa sens&#244; de arimasu:" "business is war."
IE, however, I despise for almost entirely pragmatic reasons. You've never looked at a piece of code and thought how ridiculous it is to have massive chunks of code that say, "// this for IE" and "// this for everything else?" It's vulnerable (see the links in my signature), it crashes, it's a memory hog, it's integrated with the kernel (which is a damn stupid thing to do, and makes all the other problems ten times as severe), and it's standards-incompliant. Basically, it fails every check for a decent browser, or even a decent piece of software. Also, it tries to do too much. It's a file manager, FTP client, SMB client, web browser, control panel... This just doesn't work. Microsoft aren't the only people to try this; Konqueror also tries to combine all those things into one, and likewise gets dragged down by it. It also suffers from instability, memory-hogging, and speed problems. If you ask me, it just proves the tried-and-tested UNIX philosophy: software should do one thing, and do it well.

I have no problem with VB.Net or C#. Both are suitable languages for me.VB.NET is a fine language, if oversimplified. I also have no problem with it -- but BASIC, and thus its variants, were never designed to be used for web services, unlike Java and PHP. C# is a little more suitable (unlike VB.NET, I did learn C#), but what it strikes me as is a Java clone with all the platform-independence (one of Java's main selling points) removed.
Your above critique was a little negative and not really constructive for all the developers who choose VB.Net and they are a lot.My critique was deservedly negative, because I've little positive to say on this subject. I didn't write it to help developers who've already chosen VB.NET, I was hoping to cause newbies to think twice before choosing it as their first language.
I learned ASP.Net, Java and PHP and my suggestion for someone who want to start OOP is ASP.Net because it's more suitable, easy to learn and understand quickly and completely free.VB.NET is not more suitable, as I have explained. The most suitable language, and the only one that offers any platform independence, is C#, which is certainly no easier to learn than Java (and far harder than PHP), and also less useful. While it may be free, it runs only on IIS on Windows, which is most certainly not free; in fact, it is incredibly overpriced. Of equal or greater importance is the fact that it is not Free, which means that by its very nature it will be less stable and secure than an equivalent piece of Free, open-source software.

mwinter
05-09-2006, 08:49 PM
your original suggestion of a server-side approach is far superior.I think everybody including KegExpress knew that before your post don't you think ?No, not really. You would be surprised at just how often the best solution is ignored just because an apparently simpler one is presented, no matter how inferior that alternative is.

Mike

rjmccorkle
10-11-2006, 03:41 AM
ya - i like this idea and i need it for my site.. I have a front page that i'd like to call the updated date from several external pages so i don't have to go in and always modify the front page to say what date i modified the other pages. Was this ironed out?- the javascript didn't seem to work on the page it linked to yet the prices.txt was still on the server.

thanks!
Robert
doodleprints.com

blue-mouse
05-02-2011, 08:30 PM
In fact we can do it using javascript.

Demo:
http://www.getelementbyid.com/demo/LastModified/

Simply place <span id="LastModified"></span> where you want the message to be displayed and change the path for: src="prices.txt"



<html>
<head>
<title>Last Modified</title>
<script type="text/javascript">
function getLastMod(){
var myFrm = document.getElementById('myIframe');
var lastModif = new Date(myFrm.contentWindow.document.lastModified);
document.getElementById('LastModified').innerHTML = "Prices correct as at: " + lastModif.toLocaleString();
}
</script>
</head>
<body>
<span id="LastModified"></span>

<iframe id="myIframe" onload="getLastMod()" src="prices.txt" style="display:none;">
</iframe>

</body>
</html>


Hi,
Finally, I found here what I search for few days.
I need the same but my file is "Myprogram1.exe" and is a problem with "iframe" part of code.
I need place on my www page modification date of file just "Myprogram1.exe"
This javascript works OK for anyfile1.txt but when I place my ".exe" file then when opening subpage with this javascipt code, is starting downloading of this file automatically and text with modification date is not displayed.

Can I kindly ask about little support with this "Myprogram1.exe" file?