How to read a text file from the root folder and display it in div?
Hi I have a complicated html page. I want to read a text fi开发者_JAVA百科le and display its content into a div on Page load. I can use javascript if needed. Pl. help.
Your file is on your server in California. JavaScript is running on the user's computer in Tokyo.
How is the user's computer supposed to read content from your server? It cannot. You need to use a server-side language (PHP, Ruby, Perl, ASP, etc.) on your server to dynamically compose the HTML to send to the user.
What server are you using?
You could use the jQuery $.ajax() method to load your text file.
$.ajax({
url: '<enter text file url here>',
dataType: "text",
success: function(data){
//load content to div
$("#<divId>").html(data);
}
});
Why not do it on the server?
<?php
echo '<div>' . file_get_contents('yourtextfile.txt') . '</div>';
?>
why couldn't he use jQuery or something to do an AJAX request? if the text file is on the server it should work alight.
$(function(){
$('div').load('path_to/textfile.txt', function(){
alert('data was loaded');
});
});
精彩评论