5

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"

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Chrome says "XMLHttpRequest cannot load file:///E:/Work/ReadTheCode/content/base.html. Origin null is not allowed by Access-Control-Allow-Origin." I think it's because of XMLHttpRequest crossdomain
Since you want to use it locally, the paths should be relative to the document.
Yeah, don't point to file:///E:/Work... say if your current HTML file is in the ReadTheCode folder, your load should look like this: $('#content').load('content/base.html');
I didn't make full path, it's a Chrome issue, in Firefox it works.
2

you can use something like this:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.