how a javascript function variable data is passed to another page - javascript

i have a code here but i don't know how to pass it to the other page... basically it is inside a function so before the code there is a function but i think there is no need to worry about that what i need is to change the "alert" so some kind of variable where i can grab the value and output it, and also i would like to ask because the data in that function is an array is it correct if i write it like this? var x=new Array($(this).attr('fill')); and if it is correct how will i grab the array data into the other page?
$('text').each(function(){
alert($(this).attr('fill'));
i have a working link here but in this one i still need to change the alert into a variable i can call

It's not entirely clear what you're asking, but to get data from one page to the next, you have the following options:
You can store the data on your server and each page can get the data from the server or have the server put the data into each page when the page is constructed.
You can write the data to HTML5 local storage in the browser and each page (on the same domain) can retrieve the data from HTML5 local storage.
You can write the data to a cookie in the browser and each page (on the same domain) can retrieve the data from the cookie.
You can pass data to the next page via the query string in the URL.
Javascript variables and properties of DOM objects live only for the duration of the current page. When you go to another page, the entire javascript state is thrown away and does not survive on the next page (it is built again from scratch on the next page). This is why you must store the data somewhere and then each page can retrieve the data.

Related

How to save click path as JSON?

Let's say I have a quiz written in JavaScript. There are four different possible answers to each question. The participant clicks his way through the quiz one by one, he can cancel it at any time.
Now I want to save the user's click path including the respective timestamps. My idea is to record the click path as a JavaScript object and to transfer this object to the server via AJAX and save it in a JSON file after each click. Does that make sense?
The tricky things seem to me to be (1) to update the correct object (the correct line(s) in the JSON file) within a quiz session (no session cookies) for each click from the second click and (2) to append a new object for a new quiz session, both, if possible, without reading and rewriting the entire JSON file every time.
Your opinions and ideas are appreciated.
Edit: I have control over the backend and I'm using PHP.
Now I want to save the user's click path including the respective timestamps. My idea is to record the click path as a JavaScript object and to transfer this object to the server via AJAX and save it in a JSON file after each click.
Rather than sending the object to the server via Ajax and saving it in a .json file, you can, more simply, store a JSON (including timestamps) in localStorage and update it there.
See the localStorage API:
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
localStorage is both:
more robust / persistent than keeping the data in an object
less elaborate than Ajax-ing data to the server repeatedly

Load a template page via GET request with data from the previous page?

I want to load a new template page with data inserted via the previous call.
I have a list of items generated and they link to the general item page but without php all is lost.
I was thinking about using a href="page.html#itemid" then finding the item by querying the url.
Is this overthinking it? Any easier ways to do it?
I am assuming that on the current page, you have used AJAX to load all the data you need to display on the new template page, correct?
I can think of multiple ways to do this. It just depends on the nature of your data:
Have a popup instead:
You could have the new template page be a popup (instead of a real, separate page). Grey out the main page's content (via a darker, half transparent overlay) and then have this new popup div display the data you want. This is typically used on sites that want you to sign up for their newsletter, etc.
Pass the data via URL:
If you have very small amounts of data and it isn't sensitive data, you can just pass it through the parameters. Let's say page.html is the new page template you want to populate the data with. Then just have the link on the current page be page.html?itemid=123&param2=Hi or something like that. Then, on page.html, make sure you have JS to read the parameters from the URL and display the data.
Pass the data via cookie:
Otherwise, you might just want to use JS cookies. Especially if you have a medium or larger amount of data. Store the data loaded from AJAX into cookies. If you are expecting the user to open multiple page.html templates at the same time, you might want to use an identifier and pass it to each one, such as page.html?itemid=1 and page.html?itemid=2 etc. Then have that page.html's JS look up the appropriate cookie based on the URL's parameter id.
Do be careful and use best practices if handling sensitive data.

taffyDB not save the data

I'm using TaffyDB to have a local/offline database
but unfortunately - after refreshing the browser tab - it loses the data
example:
I have this initial variable
var clist = TAFFY();
onclick event on button - it execute this statement
clist.insert({"123" , count:count , color:color , size:size});
after clicking it - and reload the browser tab , I execute this statement
alert(clist({PID : "123"}).count());//output 0
however the previous statement should output 1
but unfortunately - after refreshing the browser tab - it loses the data
Well, yeah, that's how TaffyDB works.
however the previous statement should output 1
No, it shouldn't.
TaffyDB is in-memory only. As soon as the context for your script is torn down, such as on a page reload, it's gone. If you want to persist it, you have to do that yourself.
The easiest thing to do is serialize the entire dataset as JSON and shove it in localstorage, provided it's small enough to fit there.
As per taffydb documentation, to persist data into localStorage, you can use db.store()
let db = TAFFY()
db.store('mydb')
This single function will both store the current data in-memory and retrieve previously stored data. So, if you call store at the beginning of your script, then on a window refresh, the stored data will be loaded.
BEWARE: However, the saving routine for db.store() is called as a non-blocking process... so if you wish to immediately retrieve data that you stored using some other call on localStorage, it will likely not be there. The best practice for store() is thus to call it on window load and then whenever you wish to save your existing data.

Sending data using Javascript from one web page to another in the same domain

I have a function in javascript that is generating an array of canvas elements (screenshots of the current webpage). If the user clicked on a new link, I want to save my current array and somehow send it to the new page where I will call the function again, but with the array already filled by the previous values, through the same javascript file (I want it all to remain client side).
This was for context. So my question is, can I send data to pages sharing the same domain, client side? Is there some way I can store the information maybe, and then access it later, without going server side?
If you want to share these 'canvases' between tabs/websites then you should remember that inside localstorage or cookies you can only store STRINGS. You cannot store canvas as a string (you would get something like "[object HTMLCanvasElement]" which is nonsense in this case, but you can convert them to images and then convert images to strings, which you can then store inside localstorage or cookies.

Using Global Variables between multiple functions in JQuery?

I want to dynamically load an image using jQuery like this:
main.js
var slidersrc=""; //try to define global variable - not sure if this is correct
jQuery(document).ready(function() {
jQuery("#sliderimg").attr('src', slidersrc);
});
jQuery("#selection1").click(function() {
slidersrc='wp-content/themes/*****/slide1.png';
});
So the first time user access my website, the slider is empty. After user clicks on one of the selection areas, I set the global variable value. Then if user continues to navigate at my website to different pages, the user should be shown a slider image as a result of his selection.
However, this doesn't appear to work.
Am I correctly using the global variable in jQuery? Or is there a better way to save the user selection value in client side?
thanks!
Global variables do NOT survive from one page to the next. Each page starts an entirely new javascript context (all new global variables, functions, etc...).
If you want to save state from one page to the next, your options are:
Put the data in a cookie which you can read from each successive page when that page loads.
Put the data in a browser local storage which you can read with javascript from each successive page when that page loads (recommended option).
Store the data on the server and embed it in each page as it is served from the server.
You can read about how to read and write from browser LocalStorage here and here.
If you're planning on changing the slider image each time the user clicks, then perhaps you want to save an index into an image array in local storage. When the page loads, you read the current index from localStorage (or supply a default value if no value exists in local storage), then write back the current value to localStorage for the next page. If the user takes some action that causes the index to update to a new value, then you update your page and then write that new index into localStorage so the next page can read it from there and so on.
LocalStorage is a similar concept to cookies, but it's a bit easier to manage and more efficient (the data is not sent to the server with every page request).

Categories