Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a whole html page in a div using jquery?

Tags:

html

jquery

First of all I want everyone to know that I am absolute beginner so please be patient with me.

I want to know how am I going to put the entire html page in a div. I tried $("#footballPlayers").html("footballplayers.html"); but it displays footballplayers.html text instead of the whole page.

index.html:

<html>
<head>
<script type="text/javascript" src="jquery1.6.4min.js"></script>
<script type="text/javascript">
 $(function(){
  $("div#tabFootballPlayers").click(function(){
    $("#footballPlayers").html("footballplayers.html");
    $("#actionStars").html("");
    $("#directors").html("");
  });
 });

 $(function(){
  $("div#tabActionStars").click(function(){
   $("#actionStars").html("actionstars.html");
   $("#footballPlayers").html("");
   $("#directors").html("");
  });
 });

 $(function(){
  $("div#tabDirectors").click(function(){
   $("#directors").html("directors.html");
   $("#actionStars").html("");
   $("#footballPlayers").html("");
  });
 });
</script>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div>
 <div id="tabFootballPlayers">Football Players</div>
 <div id="footballPlayers"> </div>
 <div id="tabActionStars">Action Stars</div>
 <div id="actionStars"> </div>
 <div id="tabDirectors">Directors</div>
 <div id="directors"> </div>
</div>
</body>
</html>
like image 533
NinjaBoy Avatar asked Nov 16 '11 17:11

NinjaBoy


2 Answers

use load

jQuery("#footballPlayers").load("footballplayers.html");

for more details click here

like image 120
Hitu Bansal Avatar answered Oct 18 '22 16:10

Hitu Bansal


You use the .load() function:

$("#footballPlayers").load('footballplayers.html body');

Notice the selector after the URL. You can select elements from that page. I think you need the body, as having nested <html> tags could end badly.


A few more comments about your code:

You don't use this function more than once. Just shove all of your code into it:

$(function() {
  // All of your code here.
});

I prefer this syntax, as it looks more functional and shows you what it does:

$(document).ready(function() {
  // All of your code here.
});

Also, your code is really redundant. Try condensing it:

$(document).ready(function() {
  $("#your_menu_container div").click(function() {
    $(this).load(this.id.substr(3).toLowerCase() + '.html').siblings().html('');
  });
});
like image 41
Blender Avatar answered Oct 18 '22 16:10

Blender