简体   繁体   中英

How to include data from another HTML to another without PHP

This is my first question here so try to make my best so you can understand it clearly... So my problem is that I want to load menus and footers from external HTML file so It would be easier later modify them. I can't use php or other server-side languages cause it's for school and I use it from usb stick.

My current code goes like this:

<head>
    <title>SchoolStuff</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <div id="header">
        <p>[Stuff here]</p>
    </div>
    <script > 
     $("#header").load("/header.html #header"); 
    </script> ...


and in the header.html it look like this:

<body>
<div id="header">
    <ul>
      <li><a href="#"> Just a Test</a></li>
    </ul>
 </div>

So do you guys have any ideas how to do this? It doesn't matter if you solve this in something else than jquery but it can't be server-side language. And IFrame doesn't work for this kind of purpose because I don't want the whole page. I just want specific parts.

ok it looks like it just wont work in chrome (which is really weird though) so if you have any ideas to get it work in chrome too it would be appreciated 好吧,看起来它不会在chrome中工作(虽然确实很奇怪),所以如果您有任何想法也可以在chrome中工作,将不胜感激
I decided to put my site online so it's easier for everyone... 我决定将我的网站置于在线状态,以便所有人都更容易...

This should be working:

<script > 
$(function(){
     $("#header p").load("header.html"); 
})
</script>

Without the use of server-side technologies, I don't believe this is possible. JavaScript adheres to the same origin policy, and when loading files via the file:// protocol the origin I believe is set to null .

In my testing, I'm getting the error: Origin null is not allowed by Access-Control-Allow-Origin . Which leads me to believe that you cannot load files from the local filesystem (small white lie , this may help ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM