transfer data between web pages using JSON

I'm trying to extract a JSON object with the following function:

  function getPost()
   {
      var fname = document.getElementById("fname").value;
      var lname = document.getElementById("lname").value;
          var v = {
       "name": {
           "firstname": fname,
           "lastname": lname 
       }
    };
   }

I need help figuring out how to share this JSON variable, v, between pages in my application. How can I pass this JSON data across different pages?

Answer №1

One way to store data locally in the user's browser is by utilizing localStorage or SessionStorage(learn more here). This allows you to access your stored data across multiple pages.

Here is a simple example:

localStorage.setItem('a', '{ "name" : "test"}');
eval("var myObj = " + localStorage.getItem('a'));
myObj.name; // test

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

Storing data from a JSON file into a dictionary in Discord.py can be achieved by using the

In short, I am looking to store a dictionary called user_messages in a JSON file with the format {'user': amount_of_messages}. Later on, I want to be able to update the message count for each user by opening the file and saving it back to the JSO ...

What is the best way to handle JSON data errors when a value is undefined?

I am currently working on a piece of code that displays JSON data in HTML. However, if a value is undefined, it doesn't display anything. How can I handle this error so that if a value is undefined, it will show "N/A" instead of breaking the code? ...

Exploring JQuery techniques for iterating through numerous JSON files to create dynamic quizzes

Hi everyone, I could really use some assistance right now. I am working on developing a front-end system for a quiz, but I have encountered a challenge with the back-end. The back-end provides 3 JSON files to work with. JSON1 contains category and title in ...

Guide on how to send files to an API using a form in React with MUI

I am struggling with sending an image through the front-end to my back-end application that is set up to accept data. Here is the code I have: import { useState } from 'react'; import axios from '../../config'; import { useNavigate } fr ...

Methods for resolving a ViewStyle typescript issue in react native

Trying to pass a width parameter into StyleSheet is causing an error like this: <View style={styles.children(width)}>{children}</View> Here's how I'm trying to use it: const styles = StyleSheet.create({ modalContent: { flex: ...

`Implementing implicit JSON serialization for nested objects in Play Framework 2.x`

There are two different classes named Foo and Bar. In the class Foo, there is a field that contains an instance of the class Bar. The main query here is, how can I create an implicit JSON Writes for the Foo class? Below is the provided code snippet: pac ...

Encountering ERR_EMPTY_RESPONSE when using the $.POST method

I have been struggling with a issue on my social networking site that includes chat functionality. I have utilized the $.post method extensively across multiple pages, and it generally functions well except for a specific page called message.php where user ...

A guide on transferring a Vue component to a custom local library

After successfully creating components using template syntax (*vue files), I decided to move common components to a library. The component from the library (common/src/component/VButton): <template> <button ... </button> </templat ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Tips for emptying the fields and restoring a form within a modal once it has been closed

I am facing an issue with a form inside a modal where the fields initially have red borders to indicate they are mandatory. However, when I fill out each field and then close the modal, the fields are cleared but the red borders indicating their mandatory ...

I'm curious about the reason behind the error message stating "Navbar is defined but never used" popping up while working with Vue

After importing the Navbar component from Navbar.vue, I attempted to include it in my app.vue. However, upon doing so, I encountered an error stating 'Navbar' is defined but never used. As a newcomer to Vue, I am unsure of why this issue is occur ...

Incorporating interactive buttons within Leaflet popups

I'm facing an issue with adding buttons to a Leaflet popup that appears when clicking on the map. My goal is to have the popup display 2 buttons: Start from Here Go to this Location The desired outcome looks like this sketch: ___________________ ...

Encountered ENOENT error while setting up create-react-app

Recently delved into the world of React by taking an online course on Udemy. However, I encountered a sudden issue with my create-react-app and received this error log. Installing packages. This may take a few minutes. Installing react, react-dom, and ...

Is there a way I can set a variable as global in a jade template?

I am trying to pass a global object to a jade template so that I can use it for various purposes later on. For instance: app.get("/", function(req, res){ var options = { myGlobal : {// This is the object I want to be global "prop ...

Troubleshooting Problems with Angular Directive Parameters

I'm currently working on a directive that will receive arguments from the HTML and use them to fill in the template fields. Here's the code for the directive: (function(){ angular.module("MyApp",{}) .directive("myDirective",function(){ ...

What is the method used by threejs to position a mesh within a scene when no coordinates are provided?

I'm embarking on a personal project using threejs and I'm seeking clarification on how the threejs add method functions when adding to the scene. In a small example, a scene and camera are created with near and far plane units set from 0.1 to 10 ...

Dashboard for Decoupled Content Management System with Single Page Application

While conducting research for a new project, I came across the idea of building a SPA application (using Angular) with a headless CMS (which has not yet been chosen). Despite feeling confident in my expertise, there is one key aspect that I am uncertain ab ...

Is there a way to pre-load images from component B within component A using Vue?

In a scenario where I have two Vue files, A.vue and B.vue, the issue arises when navigating from A to B. B contains numerous images fetched from Firebase realtime database, resulting in slow loading times upon first visit. To address this, I aim to preload ...

Leveraging the power of JavaScript Math methods to dictate the Co-ordinates of HTML Canvas .fillRect

Greetings to everyone! I have dedicated my entire evening to understanding how to implement the (Math.floor(Math.random()) function as the coordinates for the .fillRect method on an HTML canvas element. Despite searching through this website and various ...

Exploring the versatility of HTTP actions in Express: Using GET and

Currently, I am setting up a server through express. While everything is running smoothly with my project, I have a small query that is not related to the project itself. My confusion lies in the requirement to use the GET method when, in my opinion, usi ...