megas
megas

Reputation: 21791

How to add content from file using jQuery?

I have main html document:

<html><head></head>
<body>
    <div id="content">
    </div>
</body>
</html>

Also I have a content file (not the html document, just html code):

<div class="CodeRay">
<div class="code"><pre><span class="no">   1</span> require
....
</pre></div>

I want to add the content file to html document like here:

<html><head></head>
    <body>
        <div id="content">
            <div class="CodeRay">
            <div class="code"><pre><span class="no">   1</span> require
            ....
            </pre></div>
        </div>
    </body>
</html>

How to use jQuery to accomplish this task? Also I need to run this site localy. Thanks.

EDIT: Locally means like this "file:///E:/Work/ReadTheCode/main.html"

Upvotes: 5

Views: 14777

Answers (2)

Dan
Dan

Reputation: 3890

you can use something like this:

$('#result').load('ajax/test.html');

See: http://api.jquery.com/load/

Upvotes: 2

Vap0r
Vap0r

Reputation: 2616

Use jQuery's .load() function. The code would probably look something like this:

$(document).ready(function(){
  $('#content').load('contentFile.html');
});

If you need more info, here is the link to jquery's load documentation.

Upvotes: 9

Related Questions