Changing the entire content of a webpage from the server using AJAX

I am looking to update the entire page content with the click of a button, transitioning from Words.html to SelectNumber.html

This snippet is from Words.html

<html>
<head>
  <meta charset="UTF-8">
  <title>Number Game</title>
  <link rel = "stylesheet" href = "stylesheet.css">
</head>
<body>
  <div id = "firstScreen">

    <h1 id ="Title" class = "title">
      The<br>Number Game
    </h1>

    <input type = "image" src = "button.png" class = "button1" onclick = "loadScreen">
    <h3 class = "start">START</h3>
  </div>
</body>
<script src = "Main.js"> </script>
</html>

Below is part of the JavaScript code:

function loadScreen()
{
  var load = new XMLHttpRequest();
  load.onreadystatechange = function()
  {
      document.getElementById("firstScreen").innerHTML = this.responseText;
    }
  };
  load.open("GET", "SelectNumber.html", true);
  load.send();
}
function myFunction(load)
{
  document.getElementById("firstScreen").innerHTML = load.responseText;
}

The following content is from SelectNumber.html

<html>
<head>
  <meta charset="UTF-8">
  <title>Number Game</title>
  <link rel = "stylesheet" href = "stylesheet.css">
</head>
<body>

  <div id="Screen2">
    <p> Hello World</p>
  </div>

  <script src = "Main.js"></script>
</body>
</html>

My goal is for the full content to transition from Words.html to NumberSelect.html upon clicking the designated button.

Answer №1

function displayContent() {
  var content = new XMLHttpRequest();
  content.onreadystatechange = function() {
    document.getElementById("mainContent").innerHTML = this.responseText;
  }

  content.open("GET", "content.html", true);
  content.send();
}

function updateContent(content) {
  document.getElementById("mainContent").innerHTML = this.responseText;
}
displayContent();

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

Locate and select all visible table rows that have the class "x" within a fieldset, then mark them with

Apologies for the lack of a better title. I have successfully implemented the following code snippet: $('.checkall').unbind('click').click(function () { $(this).parents('fieldset:eq(0)').find(':checkbox').attr( ...

Error encountered in NodeJS after refreshing the node server

I am currently in the process of developing a web application using Node.js, Express framework, and EJS templates. Below is the code snippet for my server setup: const express = require('express'); const app = express(); const PORT = process.en ...

Creating an AJAX request in Knockout.js

Forgive me if this question has been asked previously, as my search attempts have been unsuccessful in finding a solution. Despite consulting the knockout documentation, I still struggle to articulate my issue effectively for searching. My situation invol ...

Utilizing Promises in the apply function

I am currently working on a project in Node.js that utilizes bluebird for promise handling, as well as ES6 native promises. In both projects, I have a chain where I make a database query structured like this: some_function(/*...*/) .then(function () ...

In search of an effortless method to effortlessly select numerous elements using a handy bookmarklet

My goal is to develop a bookmarklet that can perform various functions on different webpages with ease. The concept involves placing it in a convenient location, such as the bookmark bar, and executing a "standard function" on a particular page. Instead of ...

Unable to delete a dynamically inserted div through an event triggered by an existing div

After successfully completing an AJAX action, I attempted to remove a specific div from the DOM. Below is some of the code I used: In my attempt to remove it from the DOM, I wrote this script: $('#close-modal').on('click', function () ...

Browser refusing to acknowledge jQuery/JavaScript (bootstrap)

Here is where I include the necessary jQuery libraries and my custom jQuery code: <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script src= ...

What is the best way to display lengthy content using a pair of HTML tags?

I am facing an issue with my Angular4 app where I have a <md-card-content> tag on part of my page. Inside this tag, I am trying to display some content within a <p> tag which has a maximum height of 20cm. While this setup works fine for short ...

Transform an HTML table string into JSON format

I discovered a useful library called table to JSON that allows me to convert an HTML Table to JSON using its ID (#testtable in the example below): var table = $('#testtable').tableToJSON(); alert(JSON.stringify(table)); However, I am interested ...

I'm encountering an error while attempting to parse the XML file

Need help with ajax call $j.ajax({ url: "http://www.earthtools.org/timezone/40.71417/-74.00639", dataType: "jsonp", complete: function(data){ console.log(data); } }); The URL returns XML, but I'm trying to use JSONP to avoid cross-site s ...

Assign a function in one class to be equivalent to a function in a different class

What is causing this issue and how should it be resolved? class A { constructor() { console.log('constructin A') } public someMethod = (x: string) => { console.log(x) } } class B { private myA: A constructor ...

Is there a method for decreasing the expected conv2d_Conv2D1_input from 4 dimensions to 3?

Issue: An error message states that conv2d_Conv2D1_input is supposed to have 4 dimensions, but the array provided has a shape of [475,475,3] However: The inputShape is configured as [475,475,3] Upon inspection, tensors exhibit the shape [475,475,3] Err ...

how to enable drag and drop functionality based on names in angularjs

In my project using angular js, I have implemented a drag and drop feature that allows items to be dragged between cells in two directions. I now want to enhance this functionality by allowing members with 'a's (e.g. 1a, 2a, etc.) to be dragged ...

Repositioning SVG rectangles made with d3.js (Using d3 or jQuery for this task)

Situation: I'm currently working on integrating a Google map with a d3 overlay, inspired by Mike Bostock's example found here: http://bl.ocks.org/mbostock/899711 This project is being developed within a Rails 4 application, utilizing d3.js and ...

AngularJS: Recommendations for structuring code to dynamically update the DOM in response to AJAX requests

Within Angular's documentation, there is a straightforward example provided on their website: function PhoneListCtrl($scope, $http) { $http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); $scope.order ...

Refreshing and paginating data tables

I'm currently working on a basic CRUD data table and I have added a refresh function using AJAX to dynamically append HTML without reloading the page. Everything was working fine until I introduced pagination from Laravel. The issue now is that only t ...

This issue with ng-include not functioning properly in Chrome but working fine in Firefox

My code is running only in Firefox and not working in Chrome. HTML Code <div ng-controller="myController1"> Select View: <select ng-model="employeeview"> <option value="1.html">1</option> < ...

Conceal and reveal feature for multiple dynamic identifiers simultaneously

After submitting the form, I am dynamically creating buttons. <template name="workflow"> {{#each newaction}} <div class="btn-box" > {{> actioncardsubcontent}} <button type="button" class="cancelsub" >New Action</button&g ...

Innovative react route

Currently, I am in the process of learning about dynamic react routes. In the code example I am working on, there are different buttons for each task. The goal is to render the WorkDetails component when a button is clicked. However, it seems to not be fun ...

I did not anticipate the React setState function to behave in the manner it did

While working on form validation for my page, I came across a tutorial showcasing a validation method that seems to be functioning correctly. However, there is one aspect that I am struggling to grasp. The tutorial suggests declaring a variable before the ...