2

I know this question has been asked several times but I am experiencing problem with this. I want to append my index.html file with contents of otherpage.html

My code for index.html is

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.get("otherpage.html", function (data) {
                $("#appendToThis").append(data);
            });
        });
    </script>
</head>
<body>
    <div id="appendToThis"></div>
</body>

And my code for otherpage.html is

<p>This is second page.</p>

When I open Index.html I see a blank white page. Can anyone please point out where the mistake is..Thanks in Advance

3
  • You cannot perform ajax on filesystem Commented Feb 28, 2017 at 18:48
  • Code is working fine for me. Most likely you are getting a CORS error. Try loading it in a web server. Commented Feb 28, 2017 at 18:50
  • 1
    Does the console logs any errors? Commented Feb 28, 2017 at 18:52

4 Answers 4

1

This is another answered question that I believe is what you are looking for:

How to load another html file using JS

This example uses some of the points others were making in this thread. It will require an AJAX request for the JS to read the file, parse it, and then replace that info. The example uses an onClick method, but you should be able to use any method that you like (including on load) to execute your defined AJAX call function.

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

Comments

0

Run your file on a website or localhost. Ajax requests are disabled in chrome browser over file protocol.

You can try the following flag if you really want to run it: chrome --allow-file-access-from-files

Comments

0

Your code no problem,and the problem could be:

  1. by default browser can't load files from system via ajax,so you must run the pages in a web server.you can use a http-server,this server is simple to use and install,but first you must install nodejs.
  2. if you have run the pages on the server,and still can't load the partial page,your partial page url maybe wrong.

Comments

-1

try it hope it will work

<script type="text/javascript">
    $(function () {
       $("#appendToThis").load("otherpage.html");
    });
 </script>

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.