Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bobbravo2/0fd96d0481fcd27ab50a496fa074de94 to your computer and use it in GitHub Desktop.
Save bobbravo2/0fd96d0481fcd27ab50a496fa074de94 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Click Button with Firebase!</title>
</head>
<body>
<!-- Text with spanID that gets filled with content -->
<p>After clicking, go to the firebase database to see the click counter go up.</p>
<p>
While clicking, put this html file to the left, and show the firebase database on the right to show how firebase is real time!
</p>
<!-- Button -->
<button id="clickButton">Click Me!!!!</button>
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<!-- ================================================================================== -->
<!-- Firebase JavaScript Link -->
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
<script>
// Initialize Firebase
// This is the code we copied and pasted from our app page
var config = {
apiKey: "AIzaSyCYCUBn6igqqkTjKwQq0wLtGNuh51y6wmk",
authDomain: "bobs-awesome-project.firebaseapp.com",
databaseURL: "https://bobs-awesome-project.firebaseio.com",
storageBucket: "bobs-awesome-project.appspot.com",
};
firebase.initializeApp(config);
// Variables
// ================================================================================
// Get a reference to the database service
var database = firebase.database();
// Initializing our click count at 0
var clickCounter = 0;
// Functions
// ================================================================================
// On Click
$("#clickButton").on("click", function() {
// Add 1 to clickCounter
clickCounter++;
// ***** Store Click Data to Firebase in a JSON property called clickCount *****
// ***** Note how we are using the Firebase .set() method ****
// ***** .ref() refers to the path you want to save your data to
// ***** Since we left .ref() blank, it will save to the root directory
database.ref().set({
clickCount: clickCounter
});
// Now! go to https://fir-click-counter-7cdb9.firebaseio.com/ to see the impact to the DB
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment