Solved

Display within a DIV, content from another html file using javascript

Posted on 2009-03-29
3
1,818 Views
Last Modified: 2012-05-06
Hi experts,

I have a first html page (called "index.htm") containing 3 <div>, 1 for the header, 1 for the left menu and the 3rd one for the content. They are identified with id "haut", "gauche" and "principal". In the "haut" area I have a link "Accueil". I have also 2 links in "gauche" area "Nos modèles" and "Calcul des prix".

A second html page (called "principale.htm) contains informations with 3 anchors called "voitureVedette", "lesModeles" and "calculDuPrix".

I've tried to create a javascript function called "chargerContenu" with argument "contenuDemande" where I give the content I want to be displayed into <div id="principal">. For example, I use "onLoad" event with the <body> to load the page "principal.htm#voitureVedette" but it doesn't work.

I also want my 3 links to load into <div id="principal"> page principal.htm where:
"Accueil" loads "principal.htm#voitureVedette"
"Nos modèles" loads "principal.htm#lesModeles"
"Calcul des prix" loads "principal.htm#calculDuPrix"

NOTES TO EXPERTS:

1) I want to use javascript to achieve my code. I'm learning and want to understand what happens.

2) My javascript is not part of my document, it is refered into a link in my <head> and modified it here to give you the complete code (my head contains: "<SCRIPT LANGUAGE="Javascript" SRC="scripts/messcripts.js"> </SCRIPT>" where my function is).

Thanks,
<html>
<head>
<SCRIPT language="Javascript">
<!--
    function chargerContenu(contenuDemande){
        document.getElementById("principal").style.display = contenuDemande;
    }
// -->
</SCRIPT>
</head>
 
<body onload="chargerContenu("principale.htm#voitureVedette")>
    <div id="haut" style="position: absolute; top: 9px; left: 15px; width: 388px; height: 49px;">
		<h1>Gaug-Auto</h1>
		<a href="javascript:;" onclick="chargerContenu("principale.htm#calculDuPrix")" alt="Retour à l'accueil">Accueil</a></div>
	
    <div id="gauche" style="position: absolute; top: 120px; left: 0px; width: 170px;">
		<ul>
			<li><font face="Arial" size="4"><a href="javascript:;" onclick="chargerContenu("principale.htm#lesModeles")">Nos modèles</a></font></li>
			<li><font face="Arial" size="4"><a href="javascript:;" onclick="chargerContenu("principale.htm#calculDuPrix")">Calcul des prix</a></font></li>
		</ul>
	</div>
	
	<div id="principal" style="position: absolute; top: 80px; left: 200px;" align="center">
		<!-- allo -->
	</div>
	
 
</body>
</html>

Open in new window

0
Comment
Question by:gahute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24015261
To load one html file into another you have two options. Use an iframe and set the iframe's src attribute, or do an ajax call and append the response to the existing document.

I suggest using the jQuery (http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js) library since it will save you from dealing with all the muck of ajax calls with various browsers.

Using jQuery you could load a document using the following method
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(function() {

function loadContentWithAnchor(id, url, anchor)
{
      $("#" + id).load(url, function() {
            window.location.hash = anchor;
      });
}

loadContentWithAnchor("principal", "principale.htm", "#voitureVedette");

});

</script>

In the script above, once the document has loaded and the DOM is ready, it will execute the loadContentWithAnchor function with the three arguments. This function will load the contents of the page specified in the url into the element specified by the id argument. Once the content is loaded, it executes the function that will scroll the page down to the anchor which is supplied in the third argument.

Your links to load content would act in a similar fashion.

The "$(function() { ... });" wrapper simply tells the jQuery to wait until the document has loaded before executing the instructions within the anonymous callback function.
0
 

Author Comment

by:gahute
ID: 24081251
Hi alien109,

If I understand correctly, it is impossible to achieve what I want using only javascript?
0
 
LVL 12

Expert Comment

by:alien109
ID: 24093004
Yes, the methods I described both use JavaScript to achieve what you are looking for. The jQuery solution is pure JavaScript, the other only involves the use of an iframe and JavaScript.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question