Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Can we create a global variable called Flag? Its value will be either 0 or 1. It would be used to run a custom JS only once.
Solved! Go to Solution.
Creating a global variable is probably not the way to accomplish whatever you want to accomplish as most variables are better when they are scoped to the function in which they execute. Luckily, for the most part Canvas does not expose any global variables, so you don't have to worry about clobbering theirs -- Flag is not one of them and most JavaScript conventions call for camelCase for variable names, so they don't start with a capital letter, so Flag is really unlikely to conflict with anything that other programs might expose as well. Still, all of your JavaScript code should probably be executed within a closure
Here is a block that creates an immediately invoked function expression that runs as soon as it is encountered but wraps the variables to score the variables.
(function(){
use 'strict';
// your code goes here
})();
Now the check to only run once depends on what you want to do. I'm not assuming that you it want it once per browser, but once per user. For example, agreeing to an honor code.
In a case like this, you could use the custom data endpoints of the Users API to store the results within Canvas. Then, when a page is loaded, the script would load the custom data to decide whether or not it has been done. Once it is done, you write that to the custom data. Custom Data is not preserved across page loads, so this involves an extra API call for every page load. This is where writing the cookie can speed up the process and perhaps save making the call. If you don't want to write a cookie, you can cache the results (since it's only done once).
Depending on what you're wanting to accomplish, there may be other ways to accomplish things that don't involve custom JavaScript at all. For example, if you have control over the design and you want people to sign an honor code before beginning their course, you could have a module with that as a requirement and then have that module as a prerequisite for everything else in the course.
If you have access to the Admin -> Themes section of your Canvas instance, you can create a javascript file and upload it there.
I'm not 100% sure that I understand what you are trying to accomplish with your global flag. You can certainly create a variable in javascript by doing something like this:
var flag = 0;
if(flag == 0) {
//Run your code.
flag = 1;
}
However, this will run your code once every time a page is opened or reloaded.
If you are looking to run some javascript code once per browser, then you are probably looking for cookies, which are persisted and you can set the value once and then check to see if the value exists any other time your javascript file is run. (A good intro to cookies in javascript can be located here -> JavaScript Cookies .)
Hope this helps.
Hi @atia_kabir ,
I would echo Michael's thoughts that I would like to understand a little more about what you are attempting.
From what I have read, would I be correct in assuming you wish to have a global variable set, so that custom javascript you have created, only ever runs once when a user logs in (so not every time they load each and every page)?
On the Instructure hosted version of Canvas, that would not be possible as the Javascript does not live beyond each page load. If you use the self-hosted version of Canvas, then you could do some under the hood code modifications to achieve this.
However, @fariborzm has a great idea, if that is what you would like to achieve then Javascript Cookies would be the way to go, you could set a cookie and for the life of that cookie you could test for the cookie and then not run the code.
Of course, if someone deletes their cookies (or on cookie expiry), you would need to be prepared that the code may run again, or if the user logs in on a different device or browser. I am unsure from what you have mentioned whether it would be detrimental to have the code run more than once.
Hope that helps! With all this talk of cookies I think I might go and find a cookie *grins*.
Stuart
Creating a global variable is probably not the way to accomplish whatever you want to accomplish as most variables are better when they are scoped to the function in which they execute. Luckily, for the most part Canvas does not expose any global variables, so you don't have to worry about clobbering theirs -- Flag is not one of them and most JavaScript conventions call for camelCase for variable names, so they don't start with a capital letter, so Flag is really unlikely to conflict with anything that other programs might expose as well. Still, all of your JavaScript code should probably be executed within a closure
Here is a block that creates an immediately invoked function expression that runs as soon as it is encountered but wraps the variables to score the variables.
(function(){
use 'strict';
// your code goes here
})();
Now the check to only run once depends on what you want to do. I'm not assuming that you it want it once per browser, but once per user. For example, agreeing to an honor code.
In a case like this, you could use the custom data endpoints of the Users API to store the results within Canvas. Then, when a page is loaded, the script would load the custom data to decide whether or not it has been done. Once it is done, you write that to the custom data. Custom Data is not preserved across page loads, so this involves an extra API call for every page load. This is where writing the cookie can speed up the process and perhaps save making the call. If you don't want to write a cookie, you can cache the results (since it's only done once).
Depending on what you're wanting to accomplish, there may be other ways to accomplish things that don't involve custom JavaScript at all. For example, if you have control over the design and you want people to sign an honor code before beginning their course, you could have a module with that as a requirement and then have that module as a prerequisite for everything else in the course.
OOOH awesome, I didn't know you could do that @James you are a fountain of knowledge!
I did it in my Sorting Dashboard Course Cards script -- not the saving as a cookie, but the reading / writing using Custom User Data. It takes some figuring out, so it might be easier for people to see working code rather than starting from scratch.
To participate in the Instructure Community, you need to sign up or log in:
Sign In