Reputation: 101
There are several topics on this, but none that completely answer my question.
I would like to be able to have a link which loads a whole HTML file into a DIV.
This, for example, will load just text into my "MainBack" DIV. It works fine:
<a href="#computing" onclick="document.getElementById('MainBack').innerHTML = '<p>1</p>';">Computing</a>
but I would like to load an entire file into it like this:
<a href="#computing" onclick="document.getElementById('MainBack').innerHTML = 'HTML FILE';">Computing</a>
Any advice? I'm pretty new to this!
Upvotes: 10
Views: 77368
Reputation: 79
You can load HTML content from another file via jQuery ".load()" method.
First, in the HEAD section of your main html file, you need to include this line to access the jQuery libraries' functions:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
Second, add this code inside that HEAD section to load the contents into the DIV:
<script>
$(document).ready(function() {
$('#MainBack').load('html-content-file.html');
});
</script>
Example of the contents of html-content-file.html:
<a class="brand" href="#">240Dots</a>
<div class="navbar-content">
<ul class="nav">
<li>
<a href="index.html">Inicio</a>
</li>
<li>
<a href="quienes-somos.html">Quienes Somos</a>
</li>
<li>
<a href="servicios.html">Servicios</a>
</li>
<li>
<a href="contactar.html">Contactar</a>
</li>
</ul>
</div>
More info: http://docs.jquery.com/Ajax/load
Upvotes: 0
Reputation: 1
Use document.getElementById('element').innerHTML = 'HTML SOURCE'
with a javascript event. This works completely without problems.
Upvotes: 0
Reputation: 77251
Using the jQuery ajax API you can grab the content of a particular tag inside the document from some URL:
<a href="#computing"
onclick="$('#MainBack').load('/path/file.html body')"
>
Computing
</a>
Important:
#MainBack
is the ID of the placeholder tag in your parent document/path/file.html
id the URL for the document you want to load data frombody
is the tag holding the content you want to load.What can go wrong:
iframe
solution from the other answer.Upvotes: 7
Reputation: 8869
You can use an <iframe>
to do just that:
<iframe src="link.html" width="100%" height="300"></iframe>
Code:
<a href="#computing" onclick="document.getElementById('MainBack').innerHTML = '<iframe src=\'http://www.google.com\' scrolling=\'no\' frameborder=\'0\' width=\'100%\' height=\'600px\'></iframe>'">Computing</a>
Or you could use lightbox to do it in a really pretty manner...
It will display; photos, videos, HTML... basically anything you want.
Upvotes: 10
Reputation: 15867
<a href="#computing" onclick="document.getElementById('MainBack').innerHTML = '<iframe src=\'http://www.google.com\'></iframe>'">Computing</a>
Upvotes: 1
Reputation: 17752
You cannot load a complete html page in to another. By that, I mean that you cant have a markup like this:
<html>
<head></head>
<body>
<div>
<html>
...
</html>
</body>
</html>
Maybe thing you are searching for is an iframe? A small "window" in your page to another page?
If iframe doesn't suit you, take a look at this Jquery function which loads a html from other source in your specified selector: http://api.jquery.com/load/
Upvotes: 0