3

I am currently working on a project which allows for different webpages to be loaded into a <div> in my page when certain links leading to them are clicked , I have read up on the thread below but I do not have any idea on how to do jquery and I was wondering if the pages can be loaded with .innerHTML ? Are there ways to do it with only css and javascript and html?

Replace <div> with another HTML page

Basically what I want is something like w3.includeHTML() in which the entire page loads and show itself not within a frame but as part of the page

Here is a link to my project file and the main html page being used is index.html and the html page to linked to is greatwallofchina.html:

https://drive.google.com/file/d/1yAWZIIhBKHjwkiwwNhXUsQEVVQdjTxrM/view?usp=sharing

nTIAO
  • 415
  • 1
  • 6
  • 15
  • May be you can use the fame tag for this – Navankur Chauhan Jan 11 '18 at 06:12
  • Yes, you can use the [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) to load data with AJAX and set it in the page with `.innerHTML`. You could also use the new [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) api but it is not well supported yet and requires a [polyfill](https://github.com/github/fetch). – mattdevio Jan 11 '18 at 06:12
  • @NavankurChauhan The problem with the tag is that it is unable to work in html5 and it might not be able to run properly on browsers that uses it. – nTIAO Jan 11 '18 at 06:16
  • nothing much changes just get to know that id or class of other html page put your design changes in to style tag or by using javascript but do not forget to give "!important", this way you can change in to other html pages – Darshan Dave Jan 11 '18 at 06:20
  • Also note if the other web pages are not on the same domain, you will likely run into [Same Origin](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) issues. – Jon P Jan 11 '18 at 06:22
  • can you post your css? this can be done – Jonny Jan 11 '18 at 06:41
  • Do you want to view another html page inside of one when one clicked and another page when another is clicked? – Jonny Jan 11 '18 at 06:57
  • Yes I do want to view it that way – nTIAO Jan 12 '18 at 01:52

6 Answers6

2

If you want to show another html inside another use the object element and you can use innerHTML to achieve this. Below are 2 functions for each link one will load one page and the other will load the second. The other option requires you to post your stylesheet Hope it helps. The update to this Solution is to remove the extra scrollbar.

<script type="text/javascript">
function nextpage(){
document.getElementById('pageContent').innerHTML = "<object style=\"overflow:hidden; width: 99.25%; height: 101%\" width=\"100%\" height=\"101%\" data=\"http://localhost/test/page1.php\"></object>" ;
}
function nextpageb(){
document.getElementById('pageContent').innerHTML = "<object style=\"overflow:hidden; width: 99.25%; height: 101%\" width=\"100%\" height=\"101%\"  data=\"http://localhost/test/page2.php\"></object>" ;
}
</script>
</head>
<body style="float:left; overflow:hidden; width: 100%; height: 101%">
<nav>
<h2 class="hidden">Our navigation</h2>
<ul>
<li><a onclick="nextpage();">Home</a></li>
<li><a onclick="nextpageb();">Contact</a></li>
</ul>
</nav>
<div id="pageContent">Hello motto </div>
Jonny
  • 1,319
  • 1
  • 14
  • 26
1

You can try loading the page in your div as given below

USING JAVASCRIPT::

<div id="result">
</div>

<script type="text/javascript">
function myFunction() {
document.getElementById("result").innerHTML = '<object type="text/html" data="/path/of/htmlpage.html" ></object>';
}
<script>

USING JQUERY ::

 $(document).ready( function() {
        $("#yourLinkId").on("click", function() {
            $("#YourDiv").load("../pages/PageYouWantToShow.html");
        });
    });
Navankur Chauhan
  • 407
  • 6
  • 22
1

Do this:

not sure of the format of these "links" if you have access to them then you can use one of a couple of ways

use the HTML and any variant of the following Javascripts (JS)

HTML

<iframe id="myFrameID" src="" width="0px" height="0px" style="display:hidden;visibility: hidden"></iframe>
<div id="myPageViewerDIV"></div>

JS with selectors

$('a[href="formatoflinkhere"]').click(function () { 
    $("#myFrameID").attr("src", $("a:focus").attr("href"));
    $("#myFrameID").bind('load', function() { $("#myPageViewerDIV").html($("#myFrameID").contentDocument); });
    return false;
});

JS with any links

$('a').click(function () { 
    $("#myFrameID").attr("src", $("a:focus").attr("href"));
    $("#myFrameID").bind('load', function() { $("#myPageViewerDIV").html($("#myFrameID").contentDocument); });
    return false;
});

JS with any links but ids (the hash symbol)

$('a[href!=#]').click(function () { 
    $("#myFrameID").attr("src", $("a:focus").attr("href"));
    $("#myFrameID").bind('load', function() { $("#myPageViewerDIV").html($("#myFrameID").contentDocument); });
    return false;
});

you or other users can add on to this ^^/'

what this does:

  1. Creates a hidden invisible IFrame
  2. Binds All Links (or variant type) with the onclick event handler (returns false so no browsing to the link
  3. load the link into the source
  4. binds to the onloaded event of the iframe
  5. copys the iframe's root document to the dive of your choices...

TL;DR

Not tested, should work in theory.

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
0
$(document).ready( function() {
        $("#yourLinkId").on("click", function() {
            $("#YourDiv").load("../pages/PageYouWantToShow.html");
        });
    });
Pardeep
  • 2,153
  • 1
  • 17
  • 37
0
functionyouFunc(){
document.getElementById("element").innerHTML='<object type="text/html" data="/statics/health.html"</object>'
}
Adriaan
  • 17,741
  • 7
  • 42
  • 75
kosh
  • 1
  • 4
    Welcome to Stack Overflow! Please read [answer] and [edit] your question to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Jun 15 '22 at 10:03
-1

Its simple - Just call a javascript function on clicking the link and load your html as follows

$("#divid").load('path of file')
Hkachhia
  • 4,463
  • 6
  • 41
  • 76