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.
1 Answer
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.
6 Comments
NewToJS
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 CMSPraveen Kumar Purushothaman
@NewToJS It replaces the contents inside the
<p> tag alone. No changes to other parts. :)NewToJS
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.htmlChris
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?
NewToJS
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.
|
b.htmlfroma.htmlAdd/Edit/Delete elements, not include them. >> i want to write some javascript code to thea.htmlfile that canadd,edit, ordeletehtml code to theb.htmlfile << If this is going to be marked as duplicate I think the answer URL should be set to this: stackoverflow.com/questions/1413691/…