Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Global Variable

Featured Replies

Okay, I'm making an email client for a Uni assessment. Basically I need the input of the username to be stored globally so it can be accessed on different pages.

 

This is my javascript.

 

 

function logIn() {

 

 

var userID = document.getElementById("username").value;

var passWD = document.getElementById("password").value;

 

 

var urllogin = url1 + "checkuser.php?" + "userID=" + userID + "&passWD=" + passWD ;

 

 

if (window.XMLHttpRequest) {

request=new XMLHttpRequest();

 

} else if (window.ActiveXObject) {

request= new ActiveXObject("Microsoft.XMLHTTP");

} else alert("your browser does not support Ajax");

 

 

if(request!=null)

{

 

request.onreadystatechange = Resplogin;

 

request.open("GET",urllogin,true);

request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

request.send(null);

 

 

}

}

 

 

 

function Resplogin() {

 

 

if (request.readyState==4) {

if(request.status==200) {

var response=request.responseText;

alert(response);

 

if (response == "registered") {

 

window.location ="http://itsuite.it.brighton.ac.uk/ac315/finalmail/compose.html";

 

 

 

 

}

 

 

}

}

}

 

 

So basically I want to be able to store the username Globallyto be used on other pages. Any ideas?

In JavaScript, a global variable is one which may be accessed by any function. In your case, they are "logIn" and "Resplogin" as well as the standard "window" and "document". They aren't consistent across pages since JavaScript is predominantly a client-side script.

 

To get a variable from one page to another, you need to store it server-side. This is best done with a server-size lanauge such as PHP, JSP or ASP but there is a purely JavaScript solution: cookies. More information about them and a couple of helpful functions may be found on quirksmode

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.