1

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.

EDIT: 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
EDIT 2: I decided to put my site online so it's easier for everyone...

5
  • SSI is a good simple server-side tool to factor websites. For example, Apache supports it. Commented Mar 24, 2012 at 20:29
  • Which part of what you have doesn't work? Commented Mar 24, 2012 at 20:34
  • @Kerrek SB but problem is here that I cant use any servers so browsers have to do the job. Otherwise I would use php to do this. Commented Mar 24, 2012 at 20:34
  • 1
    Does the jQuery load function not work? Commented Mar 24, 2012 at 20:36
  • @Whymarrh It just doesn't load the contents of header.html to the index.html... Commented Mar 24, 2012 at 20:36

2 Answers 2

2

This should be working:

<script > 
$(function(){
     $("#header p").load("header.html"); 
})
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

in that case it would be same to use iframe without css styles. But the problem here seemed to be that it just doesn't work in chrome because it doesn't support to load files from local system like whymarrh stated above. And I made a domain and put the files in server so I can use PHP to do this kind of stuff(and more).
0

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).

3 Comments

Thanks... Pretty weird that this doesn't work in Chrome... I am going to put a big header to this site that it doesn't work properly in Chrome then..
@thePenahi see ogur's answer. You can't pass a CSS selector in the .load() function call. See the documentation for usage: api.jquery.com/load
@AlexW See the page you linked again! You can use CSS selectors. And this just doesn't work in chrome, the code I made works fine in Firefox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.