Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass click event between two page by localStorage

I want to take a click event from 2nd page . 1st page have a link for 2nd page, there have a button when click the button it add a HTML row on 1st page. I am trying to use localStorage for passing data. My code don't work, take a look below:

1st Page.html


HTML

<div id="content">
</div>

JS

var     output = document.getElementById('content');

addEvent(window, 'storage', function (event) {
  if (event.key == 'StorageName') {
    output.innerHTML = event.newValue;
  }
});

2nd Page.html

HTML

<input id="data" type="button" value="+" onclick="addRow()">

JS

addEvent(dataInput, 'keyup', function () {
localStorage.setItem('StorageName', this.value);
});

var dataInput = dataInput = document.getElementById('data');
object.onclick = function(){
addRow() {
var div = document.createElement('div');

div.className = 'row';

div.innerHTML = '<button>GO</button>';

document.getElementById('content').appendChild(div);
}
};
like image 687
Utpal Sarkar Avatar asked Sep 03 '17 18:09

Utpal Sarkar


People also ask

What is localStorage in JavaScript?

What is localStorage in JavaScript? It is a property that allows you to store the data in a web browser for a session. How does localStorage work? With the localStorage property, we can save the data into the browser. You can retrieve that data for the same or for any other web page.

What is the difference between localStorage and sessionStorage?

It is different from the sessionStorage JavaScript property. It saves the data for a particular session. Unlike sessionStorage, data saved using localStorage will not be deleted on the closing browser tag. This is all about a quick tutorial to pass data to another page using JavaScript localStorage.

Can I set localStorage keys in multiple windows?

We only have to be on the same origin (domain/port/protocol), the url path can be different. The localStorage is shared between all windows with the same origin, so if we set the data in one window, the change becomes visible in another one. We can also use a plain object way of getting/setting keys, like this:

What is the storage event in localStorage?

Storage event. When the data gets updated in localStorage or sessionStorage, storage event triggers, with properties: key – the key that was changed (null if .clear() is called). oldValue – the old value (null if the key is newly added). newValue – the new value (null if the key is removed).


1 Answers

You haven't defined your addRow() function properly. A function's name is defined with the keyword function, not inside of the function body. Your code:

object.onclick = function(){
addRow() {
var div = document.createElement('div');

div.className = 'row';

div.innerHTML = '<button>GO</button>';

document.getElementById('content').appendChild(div);
}
};

Should be changed to:

function addRow() {
    var div = document.createElement('div');

    div.className = 'row';

    div.innerHTML = '<button>GO</button>';

    document.getElementById('content').appendChild(div);
}
object.onclick = addRow;
like image 185
skyline3000 Avatar answered Oct 09 '22 00:10

skyline3000