Showing an object within the same page upon clicking a hyperlink

I am currently developing a web page using only HTML5 and minimal Javascript.

My goal is to have a menu displayed near the page text, allowing users to click on links that will show the contents of other HTML pages. Something like this:

https://i.sstatic.net/ravwC.png

After researching frames and iframes, I've decided to opt for using Objects instead.

Here's what I have in mind:

<center>
<right>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
</right>
<left>
<object data="text.html" type="text/html"></object>
</left>
</center>

Can anyone provide guidance on how to effectively display and change the contents from another HTML file within the same page by clicking on a menu link?

Answer №1

Here is my approach to achieving this:

<top>
  <ul id="menu">
    <li><a href="page1.html">Menu Item 1</a></li>
    <li><a href="page2.html">Menu Item 2</a></li>
    <li><a href="page3.html">Menu Item 3</a></li>
  </ul>
</top>
<bottom>
  <div id="main-content"></div>
</bottom>

$(function() {
   $("#menu > li > a").on("click",function(e) {
     e.preventDefault(); // prevent the default link behavior
     $("#main-content").load(this.href); // load content from the same origin as the script
   });
});

LATEST UPDATE
Considering that some browsers restrict ajax calls on local pages, another simple alternative is to use an iframe for your locally stored pages - this solution ensures compatibility across all browsers with no additional scripts required:

<top>
  <ul id="menu">
    <li><a href="page1.html" target="_iframe1">Menu Item 1</a></li>
    <li><a href="page2.html" target="_iframe1">Menu Item 2</a></li>
    <li><a href="page3.html" target="_iframe1">Menu Item 3</a></li>
  </ul>
</top>
<bottom>
  <iframe name="_iframe1"></iframe>
</bottom>

Answer №2

<script src="https://ajax.googleapis.com/ajax/libs/angular/1.3.8/angular.min.js"></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Demo</title>
<script language="TypeScript">
function changeContent()
{
    var element = document.getElementById("maincontent");
    if (element != null)
    {
    element.setAttribute('data', 'http://example.com/');
    alert('Content has been updated');
    }
}
</script>
</head>
<body>
<form name="Form2" method="POST">
<p><input type="button" value="Click to update content" onclick="changeContent();" /></p>
<object style="visibility: visible; border: none;" standby="loading data" id="maincontent" title="updating" width="100%" height="60%" type="text/html" data="http://example.com/"></object>
</form>
</body>
</html>

Answer №3

You have the option to add an onclick action to each of the links.

For example, based on your provided code: $("object").attr("data","text2.html");

Here is a possible implementation:

 <center>
    <right>
    <li onclick="$('object').attr('data','text1.html')">link 1</li>
    <li onclick="$('object').attr('data','text2.html')">link 2</li>
    <li onclick="$('object').attr('data','text3.html')">link 3</li>
    </right>
    <left>
    <object data="text1.html" type="text/html"></object>
    </left>
    </center>

Note that there is no 'head' tag included in this code snippet. JQuery must be utilized for this functionality to work.

This setup functions perfectly fine: http://jsfiddle.net/rws8pa8z/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason behind this being deemed as true?

Imagine we have this snippet of code: var attachRed = false; Why is attachRed = !attachRed equivalent to true? I'm curious because I'm working with Vue.js and trying to grasp why this particular piece of code functions as it does. <div id= ...

What is the correct way to insert a new key-value pair into an object within the state of functional components?

Is there a way to dynamically add key-value pairs to the initial state of an empty object in functional components, similar to computed property names in class components? I currently have an initial state of {}. const [choice, setChoice] = useState({}) ...

Combining objects while utilizing SetState within a loop: a guide

Inside my component, the state is structured as follows: class MainClass constructor(props) { super(props); this.state = { form: { startDate:"1/11/2020", endDate:"5/11/2020", .... }, ...

Unable to complete 'drawImage' operation in react-image-crop

I'm currently working on cropping an image with the 'react-image-crop' package and storing the output in the result variable. import React, { useState } from 'react' import ReactCrop from 'react-image-crop' import ' ...

Prevent Parent Handler invocation in JavaScript

I am working with an anchor structure that looks like this: <a href="#" class="someClass1"> <span><img src="expand.png"/></span> <span><a class="someClass2" href="abc.html">abc</span> </a> Both anchors ...

The information in the Vue data is not showing up on the webpage

While developing my application with vue.js, I encountered an issue where the data was not being output as expected. Strangely enough, when I logged the data to the console using console.log(), everything appeared correctly without any errors in the consol ...

Utilizing jQuery to attach events to dynamically inserted elements in the DOM

I am having an issue with dynamically adding elements to the DOM and then trying to manipulate their behavior using jQuery's .on() function. However, for some reason, the DOM is not triggering the .on() event for these dynamically added elements. You ...

AngularJS Filter from "start to finish" in values

I have been searching everywhere for a solution to this problem, but haven't found one yet. Any help would be greatly appreciated. Angular: var app = angular.module('app', []); app.controller('theController', ['$scope', ...

What is the best way to arrange this object alphabetically by name using Angular?

My client is in need of an AngularJS application that will interact with an API from an existing app. The API returns JSON data structured like the following: { "groups":{ "60":{ "name":"History" }, "74":{ "n ...

using a ternary operator within the map function

Currently, I'm developing a web application that allows users to view a list of items and mark certain items as favorites. The favorites are stored in an array of IDs assigned to a prop (this.props.favorites). To ensure there are no duplicate entries, ...

The issue of the back button not functioning in the React Multi-level push menu SCSS has

I have been developing a mobile-friendly Multi-level push navigation menu for my website using dynamically generated links based on projects from my GitHub account. I found a push menu on CodePen here and am in the process of integrating it into React inst ...

Upload files using jQuery along with additional data included

Utilizing jQuery for AJAX uploading with additional data. I am referencing the Stackoverflow code found here How can I upload files asynchronously? and implemented the following code: var formData = new FormData($('form')[0]); $. ...

Learn how to retrieve data by clicking on the previous and next buttons in FullCalendar using Vue.js

Seeking guidance on retrieving calendar data from the database for my Vue frontend, I have incorporated the fullcalendar API. Successfully able to retrieve data for the current week, however facing challenges when attempting to fetch data for the previous ...

The error message thrown in Node.js: "Cannot access 'filename' property of undefined object"

Struggling with an issue in nodejs, whenever I try to post with an image, I keep getting the error message "Cannot read property 'filename' of undefined" I've been trying to debug but can't find the root cause of the error. Can someone ...

Validate input strings in Node.js using Joi to detect and return an error if there are leading or trailing spaces

Looking to set up JOI validation in Node.js that flags errors if a string begins or ends with an empty space. For example: name = "test123" //valid name = "test(space)" or "(space)test" // invalid ...

Using the video-js feature to play multiple videos simultaneously

Is it possible to play multiple videos using video-js functionality simultaneously? The answer is yes! Check out Fiddle 1 However, an issue arises when wrapping a trigger, such as a button, around the function that invokes playVideo(). This causes the v ...

Having trouble with npm and unable to find a solution that works. Any suggestions on how to fix it?

I've been working on a JavaScript project that requires me to import a library, but I keep running into errors with npm every time I try to use it. Here is the error message: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Do you think my plan to develop an HTML parser from the ground up will be successful?

My goal is to enhance my skills by building an HTML parser. The basic idea I have in mind includes: Defining the tokenization using regex. Taking a string of HTML as input. Iterating through the HTML string. Storing details about each token, such as cont ...

Arrow functions do not function properly with Typescript decorators

I've created a typescript decorator factory that logs the total time taken to execute a function, along with the actual function execution results and parameters passed to the decorator. For example: export function performanceLog(...args: any[]) { ...

What is the reason for Javascript XMLHttpRequest returning the octet-stream MIME type response as a string instead of binary

My attempt to retrieve a gltf binary file using the XMLHttpRequest method was unsuccessful. Below is the code I used. var xhr = new XMLHttpRequest(); xhr.open("GET","THE ADDRESS",true); xhr.setRequestHeader("Accept", "application/octet-stream"); xhr.respo ...