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.
@atia_kabir
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.
<SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN><BR /> use <SPAN class="string token">'strict'</SPAN><SPAN class="punctuation token">;</SPAN><BR /> <SPAN class="comment token">// your code goes here</SPAN><BR /><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><BR /><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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:
<SPAN class="keyword token">var</SPAN> flag <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN><BR /><BR /><SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN>flag <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN><BR /> <SPAN class="comment token">//Run your code.</SPAN><BR /> flag <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">;</SPAN><BR /><SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
Canvas Data Services sent out an alert that a course was concluded (because the course was concluded) and the status of each user in the UI is set to complete, however, the enrollments API endpoint shows the status of each user in the course as active. Is this a common issue? Have I misinterpreted how the individual…
How can one track Canvas Media use without having the reports available from Canvas Studio? Checking API endpoints and the Data 2 schema, there do not seem to be dedicated Canvas Media endpoints. Am I missing some? A method I've considered is using the body field of the wiki_pages table, as this contains the html code of…
Hello, I'm trying to utilize an API call to pull completed certificates and was hoping to download and store the certs as a backup. I can successfully run the API call but it will not allow me to view the certificate unless I access the URL while signed into an account where it's "my" cert. Is there a permissions setting…
UI for weekly progression idea that rest in the Schedule tab of the **K-12 UI** of Canvas it's incomplete and I really would not take it and push it into instance but I am going to post it here because in the past people like @James && @robotcars || @Steve_25 have had terrific input in the past... maybe w/their eyes and…
I have a new React + Flask webapp. It has its own login for users to start using the app. I have added this as an external tool in Canvas. What I want initially is to see of canvas users can start using the webapp without having to login if they click on the external tool link in a canvas course. I have 2 methods…