Sharing data between pages in JavaScript using arrays minus seemingly overused methods like sessionStorage or localStorage

Can someone share a method to transfer an array value to another page without relying on sessionStorage or localStorage? I would appreciate any insights!

Answer №1

If you have a webpage and need to transfer data to another page when a button is clicked, you can achieve this with the following approach:

function sendDataToPage(url, data) {
  window.location = url + "#" + JSON.stringify(data);
}

function retrieveData() {
  if (window.location.hash == "")
    return "";
  try {
    return JSON.parse(window.location.hash.substr(1));
  } catch(err) {
    return "";
  }
}

To send the data using these functions, you can use the following code snippet:

<button onclick="sendDataToPage("anotherpage.html", [4, 5, 6])">Transfer Data</button>

In the receiving page, you can access the transferred data by calling retrieveData().

Answer №2

If you want to pass an array, consider using the query string method.

For more information on how to pass an array within a query string, check out this helpful link: How to pass an array within a query string?

To achieve passing the array from a.html to b.html, utilize the `beforeunload` event to place the array in the query string before navigating to b.html. Then, use the `DOMContentLoaded` event in b.html to retrieve the array.

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

Retrieving a compilation of items found within text selected by the user on a website

Imagine a scenario in which a webpage contains the following structure: <p> <span class="1">Here's some text</span> <span class="2">that the user</span> <span class="3">could select.</span> </p> I ...

Protractor - How to Locate Ui-Sref Links

I am encountering an issue while trying to detect the links in the following HTML code. Despite installing linkUiSref through npm, I continue to receive an error stating "object has no method linkUisref." Any suggestions on how to resolve this? element(by ...

Is there a way to determine using code whether or not an iframe will be blocked by a website?

I'm creating a script that dynamically generates multiple iframes loading random websites. I am curious if there is a way to check programmatically if a website blocks being iframed, so I can display a thumbnail of the site as an alternative. Can this ...

Looping through a JSON object within the server environment

If I have a basic model set up like this: public class User { public string ID { get; set; } public string Name { get; set; } public string Age { get; set; } } and let's say I receive a JSON object structured as follows: var users = { ...

What is the mechanism by which AngularJS manages all of my module dependencies with jest?

Updated: Since this is an older project, I didn't use any module loader. Instead, I imported all dependencies in my index.html using the script tag. The structure of my AngularJS looks like this: app.js angular.module('app', ['Loca ...

retrieve the item that contains a vacant array within a specific record

I have a set of nested object documents that I need to retrieve and group based on the nested objects with empty arrays using aggregation. Let me provide a more detailed explanation-- suppose my collection consists of the following documents -- Document ...

What could be causing the z-index property in CSS to fail, despite the fact that one component has a higher z-index value than the other target

Even though the z-index value of the product component is higher than that of the home-image, the home-image still dominates and overshadows the product component. In this specific scenario, the product has a z-index of 1 while the home-image has a z-index ...

Prevent repetitive content on your Node.js server

After realizing my small image hosting has many duplicate content, I am looking for a solution to prevent this issue in the future. My idea is to use either checksum or hash code so that whenever a new file is uploaded, it will be hashed and compared with ...

Audible crackling noises from the web audio panner occur when adjusting the position

Recently, while working on a 3D web app that involves positional 3D audio, I encountered an issue with crackling noise in the output as the sound source changes position. At first, I suspected it could be related to programming or library problems (specifi ...

Non-encrypted GWT HTML class tag styles

I have inherited an old xsl transformation that generates plain HTML, which is then bound to a widget using a GWT HTML field. The current setup looks like this: HTML html = new HTML html.setHTML(result) In the UiBinder section of the widget, there are so ...

Best practices for alerting using React and Redux

As I delve into Redux for the first time and work on revamping a fairly intricate ReactJS application using Redux, I've decided to create a "feature" for handling notifications. This feature will involve managing a slice of state with various properti ...

Google's search functionality utilizes XHR requests for suggesting search results

One day, I decided to conduct an experiment using Google and Firebug. With my firebug open, I typed "in" into the search bar on Google. To see what was happening behind the scenes, I checked the "NET" tab in Firebug and noticed that several new GET request ...

Sending a picture through AJAX using the camera feature of p5.js

Currently, I am exploring the camera functionality of p5.js to capture video. However, I am facing a challenge when trying to upload these images to a server using ajax. I am unsure about how to convert a p5.js Image object into a suitable format for trans ...

Is it possible to convert an object with properties of equal length into a list of objects using JavaScript?

I am working with an object that has multiple keys, each containing a list of equal length: myobj = { 'key1' : [1, 2, 3], 'key2' : ['a', 'b', 'c'], 'key3' : [true, false, true], .. ...

Dynamic Data Loading into Card (Using Javascript and Firestore)

Currently, I am in the process of constructing a KDS (Kitchen Display System) that utilizes javascript & firestore to display orders. The orders are presented using cards, but I'm encountering an issue where all the order items are appearing in th ...

Matching Tables with JavaScript and JSON

After hours of coding, I'm stuck on a simple task and could really use some assistance. The "users" object contains user account information, with the function "get" meant to retrieve matching objects from this array. var users = [ { name ...

Centering a JavaScript object within a div

I recently delved into the world of JavaScript and decided to experiment with this graphing library. I followed one of their example codes, and now I have a bit of a beginner-level HTML/JS question. This is the code snippet they provided: Upon running t ...

Greetings all! I am curious about the strange error I encountered in the psql terminal

I am able to add new users using Postman with this model sequelize.define('users', { id: { type: S.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true ...

Vue: Clear the current form selection with a button click

<b-row class="mb-3"> <b-col> <div class="float-right"> <b-form-select v-model="selected" :options="options" ></b-form-select> ...

The onPress function seems to be malfunctioning within the menu interface specifically on iOS devices

I am experiencing an issue where a problem only occurs on iOS devices, but Android works fine. My menu has a userMenu class. import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu'; class UserMenu extends React.Component { ...