I\'m creating a blog Website project using Firebase.
What I want is - Create an entirely new Page for the Blog Posts once a USER creates it.
FOR EXAMPLE -
The following HTML page shows how you can query the Firestore database when the HTML page is opened, based on a value passed as a query string parameter.
So, you should do as follows:
blogPosts
in your Firestore databasebest-watches-in-2019
and a field named field1
where you enter any string valuebest-watches-in-2019
as query string parameter as folllows: http://yourdirectory/yourpagename.html?contentId=best-watches-in-2019
.<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"
></script>
<script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-firestore.js"></script>
</head>
<body>
<span id="field1"></span>
<script>
var config = {
apiKey: 'xxxx',
authDomain: 'xxxx',
projectId: 'xxxx'
};
firebase.initializeApp(config);
$.extend({
getUrlVars: function () {
var vars = [],
hash;
var hashes = window.location.href
.slice(window.location.href.indexOf('?') + 1)
.split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function (name) {
return $.getUrlVars()[name];
},
});
$(document).ready(function () {
var documentID = $.getUrlVar('contentId');
firebase
.firestore()
.collection('blogPosts')
.doc(documentID)
.get()
.then(function (doc) {
if (doc.exists) {
$('#field1').text(doc.data().field1);
} else {
// doc.data() will be undefined in this case
console.log('No such document!');
}
})
.catch(function (error) {
console.log('Error getting document:', error);
});
});
</script>
</body>
</html>