最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace innerhtml with external page - Stack Overflow

programmeradmin1浏览0评论

I have a page that consists of a header div for navigation, a content div for content, and a footer div that just has some static information.

The idea is that when you click on something in the header bar, the content div changes its content accordingly. I'd like to store each content element in its own file, for ease of development. How can I go about filling in this magicLoad function I've written below?

I'm currently not using any framework (jquery es to mind) but I'm certain not opposed to using one. If there's other best practices I'm violating here, I'm keen to know as well, but my primary goal is to load from an external page. How can I do this?

Here's a basic skeleton mockup of my setup right now. (There's more than just those 3 of course, but those are enough to get the point across.)

<html>
    <script type="text/javascript">
    function show(which) {
        document.getElementById(which).innerHtml = magicLoad(which + ".html");
        return false;
    }       
    function showHome() { show('home'); return false;} // for onload

    </script>
    <body>
    <div id="header">
        <a href="javascript:void(0)" onclick="show('home')">HOME</a> |
        <a href="javascript:void(0)" onclick="show('training')">TRAINING</a> |
        <a href="javascript:void(0)" onclick="show('audits')">AUDITS</a>
    </div>
    <div id="content">

    </div>
    <div id="footer">

    </div>
    </body>
    <script> window.onload=showHome; </script>
</html>

I have a page that consists of a header div for navigation, a content div for content, and a footer div that just has some static information.

The idea is that when you click on something in the header bar, the content div changes its content accordingly. I'd like to store each content element in its own file, for ease of development. How can I go about filling in this magicLoad function I've written below?

I'm currently not using any framework (jquery es to mind) but I'm certain not opposed to using one. If there's other best practices I'm violating here, I'm keen to know as well, but my primary goal is to load from an external page. How can I do this?

Here's a basic skeleton mockup of my setup right now. (There's more than just those 3 of course, but those are enough to get the point across.)

<html>
    <script type="text/javascript">
    function show(which) {
        document.getElementById(which).innerHtml = magicLoad(which + ".html");
        return false;
    }       
    function showHome() { show('home'); return false;} // for onload

    </script>
    <body>
    <div id="header">
        <a href="javascript:void(0)" onclick="show('home')">HOME</a> |
        <a href="javascript:void(0)" onclick="show('training')">TRAINING</a> |
        <a href="javascript:void(0)" onclick="show('audits')">AUDITS</a>
    </div>
    <div id="content">

    </div>
    <div id="footer">

    </div>
    </body>
    <script> window.onload=showHome; </script>
</html>
Share edited Jul 5, 2013 at 10:55 tpimh 5782 gold badges11 silver badges32 bronze badges asked Jun 20, 2012 at 5:06 corsiKacorsiKa 82.7k26 gold badges159 silver badges209 bronze badges 3
  • You're looking for Ajax. – Madara's Ghost Commented Jun 20, 2012 at 5:07
  • @Truth no, ajax would be asynchronous. This is entirely synchronous. If I wanted someone else to control the content, then Ajax would be a great solution. – corsiKa Commented Jun 20, 2012 at 5:42
  • Ajax can very much be synchronous. – Madara's Ghost Commented Jun 20, 2012 at 5:49
Add a ment  | 

2 Answers 2

Reset to default 4

use the following, simple and straight

$('#which').load('test.html');

if which is ing as parameter

$('#'+which).load('test.html');

If you want to embed another webpage in one, you should think of using <iframe>. Something like:

<script type="text/javascript">
function show(which) {
    document.getElementById(which).src=which+".html";
    return false;
}       
function showHome() { show('home'); return false;} // for onload

</script>
<iframe id="home"></iframe>
发布评论

评论列表(0)

  1. 暂无评论