Zsolt Kindla
Zsolt Kindla

Reputation: 19

How to pass variable to a new page using pop up window?

I have got a function which turning the Id of the clicked element into a variable, then a new window opens up with a new page. How can I access/use the variable on that new page?

var idToWrite = [];

$(function(){
    $(".szlink div").bind("click", function(){
    idToWrite.push($(this).attr("id")); 
    window.open("itemEncJS.html");
    //do something
});   

Upvotes: 0

Views: 118

Answers (2)

A7S
A7S

Reputation: 141

you can use local storage: on current page:

var storage = window.localStorage;
storage.setItem('id', id);

on the new page:

var storage = window.localStorage;
var id = storage.getItem('id');

Upvotes: 0

Anupam Majhi
Anupam Majhi

Reputation: 124

It is not safe and it is not meant to use plain html to process requests from urls.

However if you really want to do it, you can open the popup with the url with the values like this:

window.open("itemEncJS.html?name1=value1&name2=value2");

Then in the second page you will have to use regex to pick the value from url, something like this:

url = window.location.href;
nameValuePairs = url.match(/[\w]*=[\w]*/ig)
values = {}
nameValuePairs.forEach(function(pair){
  nv = pair.split("=");
  values[nv[0]] = nv[1];
})

Now you can use values to get your required values e.g

val1 = values[name1]
val2 = values[name2]

Upvotes: 1

Related Questions