I'm making a website where users submit links for everyone to view.So I was wondering if there was a way to create a variable that's stored on my website, for all users to see(kind of like cloud variables in scratch).Also if you could, please tell me how to do this in java script.
-
You would need to *store* those values somewhere that all users could access. Either an internet service of some kind or on your server that hosts the website. – David May 23 '16 at 18:44
-
What is your backend? JavaScript can't do it. That would create huge security issues. You should use a database. – 4castle May 23 '16 at 18:44
-
1@4castle: Well, JavaScript *can* do it if the server is running JavaScript, such as node.js. – David May 23 '16 at 18:45
-
@David Does node.js have site-wide globals like that? I've never seen that feature before in a backend script, but it may exist somewhere. – 4castle May 23 '16 at 18:46
-
I suppose you could use meteor.js with the built in mongoDB support and do auto pub/sub and not lock down your data source... this is like 1000% not recommended, but I suppose you could do it that way if you absolutely wanted to. That would be a "just javascript" solution, though – mhodges May 23 '16 at 18:51
1 Answers
You cannot do this with just javascript. Here's why.
Javascript runs on the client browser. Every visitor to your website will run javascript code on their own computer, with no communication with the server (except through AJAX, which sends data from javascript back to a server-side file and then (optionally) returns new data from the server to the javascript file) The point is, even AJAX uses server-side code.
Every new visitor views the website from the server. So, there must be a way for a user's changes to be saved to the server so that they can be shown to new visitors.
We are back to server-side code as being the only way to accomplish what you desire. The two things you must research are HTML <form>
s (which navigate away from (or refresh) the current page), and AJAX (which does neither).
A bit more information on AJAX
To store the information on the website so that new users can see it, you have two options: database (e.g. MySQL), or text file on the server. Your website code would then read from the database or file and incorporate that data into the website.
-
-
@gibberish It was a joke. [server-side javascript *is* a thing](https://nodejs.org/en/) – Madara's Ghost May 27 '16 at 13:07
-
@MadaraUchiha Thanks! It's a bit embarrassing to be the last one to get the joke, but certainly better than not getting it at all. Appreciate your kindness in bringing me up to speed. :) – cssyphus May 27 '16 at 18:42