Skip to content Skip to sidebar Skip to footer

How To Pass Values Form One Html Page To Another Html Page Javascript?

How can I pass values from one html page to another html page javascript For example: Page1.html page

Solution 1:

Assuming your two pages are on the same domain, you can use localStorage.

Popup.html:

var user = prompt('user name', '');
var password = prompt('password', '');
localStorage.user = user;
localStorage.password = password;

Then back in the main page:

var user = localStorage.user;
var password = localStorage.password;

Note 1: I won't comment on security (other than to say of course don't do it like I've done it above!)

Note 2: localStorage is only about 92% supported: http://caniuse.com/namevalue-storage

Solution 2:

You can refer to the window that opened your new window (called the parent window) via window.parent and then execute your javascript using that object.

Here is an example of forcing a parent page to refresh using this: [1]Forcing parent page refresh through javascript

So in your case you could have say variables for username and password in your js like this and you could set them in the popup window like so:

//parent window
username = "";
password = "";

....

//popup windowwindow.parent.username = enteredUsername;
window.parent.password = enteredPassword;

Solution 3:

If Main.html has opened Popup.html with window.open(), then the Popup.html page can have a reference to its opening window (the window of Main.html) using window.opener. It could thus call a setCredentials callback function defined in Main.html using

window.opener.setCredentials(...);

Example:

in Main.html:

// callback function called by the popupfunctionsetCredentials(login, password) {
    // do what you want with the login and password
}

in Popup.html

// function called when the form is submittedfunctionlogin(login, password) {
    window.opener.setCredentials(login, password);
    window.close();
}

Post a Comment for "How To Pass Values Form One Html Page To Another Html Page Javascript?"