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
At a technical level, if a user exists in two separate Canvas instances, is this a problem a Canvas Trust can address? If so, how? Can you add a trust(ed) login to an existing user account? Most of the (very limited) documentation I've found seems to focus on Trusts operating with enrollments, not users, but it does seem…
Hi everyone, I am seeking some insight into how Canvas LMS handles HTTP errors and how they are reflected in the logs versus the user interface. We are currently observing the following behavior in our environment: Log-only errors: We are seeing 500 (Internal Server Error) and 503 (Service Unavailable) errors recorded in…
I looked through the LTI Variable Substitutions documentation and while there are variable substitutions available for Canvas.term.name and Canvas.term.id (which is the Canvas incremented ID like "104"), I was surprised to see there was no option for Canvas.term.sisSourceId I tried testing other possible variants that may…
Summary:The Learning Mastery Gradebook allows drag-and-drop reordering of outcome columns, and this triggers a call to /api/v1/courses/:course_id/assign_outcome_order. The API returns 204 No Content (success), but the column order does not persist after page refresh. Steps to Reproduce: Open Learning Mastery Gradebook for…
I'm building a tool for my institution that imports grades from another LTI tool into Canvas so instructors can automate late assignment policies not otherwise supported by Canvas. I'm trying to use the LTIv1.3 ID which is readily available on the LTI-side as a link to the student record on Canvas, but I can't find the ID…