Click here to Skip to main content
15,899,679 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: Chatbot js code Pin
Dominic Burford6-Sep-16 2:51
professionalDominic Burford6-Sep-16 2:51 
AnswerRe: Chatbot js code Pin
ZurdoDev6-Sep-16 6:06
professionalZurdoDev6-Sep-16 6:06 
QuestionHTML and javascript dashboard Pin
Member 1271309731-Aug-16 3:21
Member 1271309731-Aug-16 3:21 
SuggestionRe: HTML and javascript dashboard Pin
Nathan Minier31-Aug-16 8:12
professionalNathan Minier31-Aug-16 8:12 
AnswerRe: HTML and javascript dashboard Pin
W Balboos, GHB14-Sep-16 4:48
W Balboos, GHB14-Sep-16 4:48 
SuggestionRe: HTML and javascript dashboard Pin
Harpreet05Kaur26-Sep-16 1:50
Harpreet05Kaur26-Sep-16 1:50 
AnswerRe: HTML and javascript dashboard Pin
Harpreet05Kaur26-Sep-16 1:24
Harpreet05Kaur26-Sep-16 1:24 
QuestionHow can I swap only body element between HTML pages? Pin
lino_7630-Aug-16 8:08
lino_7630-Aug-16 8:08 
I have two separate HTML files (index.html and home.html) and a javascript (bodyswapscript.js) file. I'm trying to figure out how to swap in (only the body) of home.html into the body of the index.html by using java script. Basically swapping 'body' elements between HTML files.

I have posted my html code and java script below. The html is quick and dirty, so I'm only interested in swapping out the body information of index.html with home.html. I also would like to keep the unordered/list items as my header and be able to use the browser back button to go back to previous pages.

1.) index.html

HTML
<head>
        <meta name="generator" content=
              "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
        <meta charset="utf-8">

        <title> My Profile</title>
        <link rel="stylesheet" href="style.css" type="text/css">    
</head>

<header>
    <hgroup>
        <center><h1>Index</h1>
        <h4>index page</h4></center>
    </hgroup>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="bodyswapscript.js"></script>


<nav>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a id="homeContainer" href="home.html">Home</a></li>
                <!-- <li><a id="testContainer" href='test.html'>test</a></li>
                <li><a id="pageContainer" href="page.html">page</a></li> -->
            </ul>
        </div>
    </div>

</nav>

</header>
<body>
    <article>

  <div id='swapcontent'>
  index page
  </div> 

</article>
    </body>
<footer>
    <p>© copy right, all rights reserved.</p>
</footer>


2.) home.html
HTML
<header>
    <hgroup>
        <center><h1>home.html</h1>
        <h4>text</h4></center>
    </hgroup>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

</header>
<body>
    <article>

        <div id='swapcontent'>
            This is my home page
        </div> 

    </article>
</body>


3.) bodyswapscript.js

JavaScript
$(document).ready(function() {
  $('li>a[id$="Container"]').click(function(event) {
    event.preventDefault();
    var href = $(this).attr('href');
     alert("Loading " + href);
    $('body').load(href + " #body", function() {
      $("#body *:first").unwrap()
    });
    return false;
  });
});



4.) css

CSS
* {
    font-family: Lucida Sans, Arial, Helvetica, sans-serif;
}

body {
    width: 720px;
    margin: 0px auto;
}

header h1 {
    font-size: 36px; 
    margin: 0px;
}

header h2 {
    font-size: 18px; 
    margin: 0px; 
    color: #888;
    font-style: italic;
}

nav ul {
    list-style: none; 
    padding: 0px; 
    display: block;
    clear: right; 
    background-color: #2B60DE;
    padding-left: 4px; 
    height: 24px;
}

nav ul li {
    display: inline; 
    padding: 0px 20px 5px 10px;
    height: 24px; 
    border-right: 1px solid #ccc;
}

nav ul li a {
    color: #EFD3D3; 
    text-decoration: none;
    font-size: 13px; 
    font-weight: bold;
}

nav ul li a:hover {
    color: #fff;
}

article > header time {
    font-size: 14px; 
    display: block; 
    width: 26px;
    padding: 2px; 
    text-align: center; 
    background-color: #993333;
    color: #fff; 
    font-weight: bold; 
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px; 
    border-radius: 6px; 
    float: left;
    margin-bottom: 10px;
}

article > header time span {
    font-size: 10px; 
    font-weight: normal;
    text-transform: uppercase;
}

article > header h1 {
    font-size: 20px; 
    float: left;
    margin-left: 14px; 
    text-shadow: 2px 2px 5px #333;
}

article > header h1 a {
    color: #993333;
}

article > section header h1 {
    font-size: 16px;
}

article p {
    clear: both;
}

footer p {
    text-align: center; 
    font-size: 12px;
    color: #888; 
    margin-top: 24px;
}


modified 30-Aug-16 14:55pm.

AnswerRe: How can I swap only body element between HTML pages? Pin
Richard Deeming30-Aug-16 8:34
mveRichard Deeming30-Aug-16 8:34 
AnswerRe: How can I swap only body element between HTML pages? Pin
Nathan Minier31-Aug-16 1:55
professionalNathan Minier31-Aug-16 1:55 
QuestionCollect text and images from image pdf using JS or AngularJS Pin
Member 1270857628-Aug-16 22:47
Member 1270857628-Aug-16 22:47 
AnswerRe: Collect text and images from image pdf using JS or AngularJS Pin
Richard MacCutchan28-Aug-16 23:00
mveRichard MacCutchan28-Aug-16 23:00 
GeneralRe: Collect text and images from image pdf using JS or AngularJS Pin
Member 1270857629-Aug-16 0:18
Member 1270857629-Aug-16 0:18 
QuestionJavascript reverse help Pin
313help23-Aug-16 3:29
313help23-Aug-16 3:29 
AnswerRe: Javascript reverse help Pin
Richard Deeming23-Aug-16 4:35
mveRichard Deeming23-Aug-16 4:35 
GeneralRe: Javascript reverse help Pin
313help23-Aug-16 4:59
313help23-Aug-16 4:59 
Questionangular http post doesnt work on IIS Pin
Member 1103130422-Aug-16 19:34
Member 1103130422-Aug-16 19:34 
AnswerRe: angular http post doesnt work on IIS Pin
Richard Deeming23-Aug-16 1:43
mveRichard Deeming23-Aug-16 1:43 
GeneralRe: angular http post doesnt work on IIS Pin
Member 1103130423-Aug-16 19:28
Member 1103130423-Aug-16 19:28 
GeneralRe: angular http post doesnt work on IIS Pin
Richard Deeming24-Aug-16 1:47
mveRichard Deeming24-Aug-16 1:47 
GeneralRe: angular http post doesnt work on IIS Pin
Member 1103130424-Aug-16 2:34
Member 1103130424-Aug-16 2:34 
AnswerRe: angular http post doesnt work on IIS Pin
2374130-Aug-16 8:18
2374130-Aug-16 8:18 
QuestionJavascript chalange Pin
Asith Raj22-Aug-16 19:28
Asith Raj22-Aug-16 19:28 
QuestionRe: Javascript chalange Pin
Harpreet05Kaur26-Sep-16 1:52
Harpreet05Kaur26-Sep-16 1:52 
QuestionAnswer Pin
Member 1269199818-Aug-16 6:28
Member 1269199818-Aug-16 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.