-1

I have 2 html files a.html and b.html and i want to write some javascript code to the a.html file that can add, edit, or delete html code to the b.html file.

Chris
  • 7
  • 1
  • 1
    So we know what you wish to achieve, now lets have a look at what you have tried? Where is your source code (Attempts) and what isn't working? Where is the issue? – NewToJS Aug 17 '15 at 09:32
  • You might find this helpful http://stackoverflow.com/questions/1413691/how-do-i-edit-html-files-directly – NewToJS Aug 17 '15 at 09:51
  • Why is this marked as duplicate for **Include** html page? The OP isn't asking how to include anything. The OP is asking how to modify `b.html` from `a.html` Add/Edit/Delete elements, not include them. >> i want to **write some javascript code to the `a.html` file** that can `add`, `edit`, or `delete` html code **to** the `b.html` file << If this is going to be marked as duplicate I think the answer URL should be set to this: http://stackoverflow.com/questions/1413691/how-do-i-edit-html-files-directly – NewToJS Aug 17 '15 at 10:07

1 Answers1

0

You have only one choice to use AJAX. If you are using jQuery, you can do something like:

$(function () {
  $("#b").load("b.html");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>I am in <code>a.html</code></p>
<p id="b">B comes here</p>

The above script is basically an AJAX request to load the contents of b.html that is found along with the a.html in the same directory, inside the <p> with an id of b.

Sorry, I forgot to mention. To make the AJAX call easier, I have added a library called jQuery, which provides a lot of functionality.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226
  • This isn't going to edit or delete parts of the html file though is it? This is just going to load `b.html` into `a.html` I think what the OP is wanting is `a.html` to be the editor for `b.html` like a CMS – NewToJS Aug 17 '15 at 09:33
  • @NewToJS It replaces the contents inside the `

    ` tag alone. No changes to other parts. `:)`

    – Praveen Kumar Purushothaman Aug 17 '15 at 09:34
  • Exactly, so `a.html` isn't adding/editing/removing existing content in `b.html`. This is just setting the innerHTML of the paragraph tag in `a.html` with the content from `b.html` – NewToJS Aug 17 '15 at 09:36
  • I haven't used jquery or AJAX before so i just copy this? There isn't another way so I can also edit or remove code? – Chris Aug 17 '15 at 09:38
  • This also requires jQuery which isn't tagged in the question so I think it would be very relevant to point out how to link to the jQuery library as people who are new to development may not know how to do so hence it not being tagged. – NewToJS Aug 17 '15 at 09:41
  • @Chris if you want to alter existing source code I would recommend researching some server-side languages. See which one you find best to use. – NewToJS Aug 17 '15 at 09:43
  • @Chris Please accept the answer if it helps you! `:)` – Praveen Kumar Purushothaman Aug 17 '15 at 09:49