/    Sign up×
Community /Pin to ProfileBookmark

Displaying a variable from file

I have a text file named sample.txt. Inside of sample.txt is the value10/20/2008. I want to call that file using javascript to display that value at the top of my page.

For example – if the date stored in the sample.txt was 10/20/2008, at the top of my page I want it to display:

Page last updated: 10/20/2008

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienOct 27.2008 — You would want to use AJAX, and this text file would need to be stored at the same domain as the page that displays the text in the file.
Copy linkTweet thisAlerts:
@ryanmkiltauthorOct 27.2008 — I only want to use javascript and HTML by doing this. I'm almost positive there is a way.
Copy linkTweet thisAlerts:
@toicontienOct 27.2008 — You can use a hidden IFRAME and set its src attribute to the text file. Again, the TXT file must come from the same domain as the web page.

Once you've got the IFRAME pointing to the TXT file, give the IFRAME a name. Then use JavaScript to get the innerHTML of the IFRAME's document object, and that should be what you need.

BTW, AJAX is JavaScript. It's just a JavaScript programming pattern.

Another thought, seeing as some browsers might prompt the user to download the TXT file in the IFRAME, is to save it as an HTML file. Then you're sure to be able to read the HTML file with JavaScript.
×

Success!

Help @ryanmkilt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...