User13839404
User13839404

Reputation: 1813

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 file and display its content into a div on Page load. I can use javascript if needed. Pl. help.

Upvotes: 1

Views: 4624

Answers (4)

heymrcarter
heymrcarter

Reputation: 678

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');
    });
});

Upvotes: 0

Ketan
Ketan

Reputation: 4280

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);
  }
});

Upvotes: 1

Phrogz
Phrogz

Reputation: 303530

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?

Upvotes: 2

JakeParis
JakeParis

Reputation: 11210

Why not do it on the server?

<?php
echo '<div>' . file_get_contents('yourtextfile.txt') . '</div>';
?>

Upvotes: 0

Related Questions