paulmezza
paulmezza

Reputation: 101

How to pass a value to the HTML page with JavaScript redirection?

I have the following function:

function loginUser(form){
  try {
    var user = Backendless.UserService.login(form.userid.value, form.pswrd.value);
    if (user != null) {
      window.location.href = "home.html";
    } else
      window.alert("Login failed");
    }
  } catch (e) {
    window.alert("Login failed. " + e.message);
  }
}

When I redirect to home.html, the user object has various fields and I want to pass the value of user.credits to an input box on the HTML page. How would I go about doing this?

This is what my HTML page looks like:

<div>Credits <input id="credits" type="text" class="input-block-level" placeholder="Credits" name="credits"></div>

Upvotes: 1

Views: 3857

Answers (2)

paulmezza
paulmezza

Reputation: 101

Yes that worked thanks...

window.location.href = "home.html?credits="+user.credits;

and on your home.html :

document.getElementById('credits').value=getUrlVars()["credits"];

just needed the getUrlVars function as well

Upvotes: 0

Burki
Burki

Reputation: 1216

you coud do

window.location.href = "home.html?credits="+user.credits;

and on your home.html :

document.getElementById('credits').value=getUrlVars()["credits"];

You may want to read up a bit on the concept of GET and POST variables. Also, notice this method is by no means safe: the user can easily manipulate the value.

A safer way would be to POST the values, or, better still, to read them, for example via PHP, on the target page.

Upvotes: 3

Related Questions