Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

How to include a html file inside another html file with Vanilla Javascript?

I want to create a single page web application with pure Vanilla Js, not React Js or another. What I want is when I click on a menu link, I want it to include another html file and show up the result without reloading. I did it with include(), get(), load() in jQuery. But I want to do it with pure vanilla Js, if possible, even though with some tricks.

Here is the one of the things I did with jQuery:

$('.link-about').click(function(){
 $('.my-div').load('about-page.html');
});

As shown above, how it should work is that I click a link and another html file loads.

question from:https://stackoverflow.com/questions/65617704/how-to-include-a-html-file-inside-another-html-file-with-vanilla-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this. I am considering elements has id

<script>
    document.getElementById('link-about').onclick = function() {
        document.getElementById('my-div').innerHTML = '<object data="about.html" >'
    }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...