I have a javascript file called pendingAjaxCallsCounter.js with a variable "var pendingAjaxCalls" which is incremented/decremented when various methods in the js file are called.
Separately, I have created an automated testing app which checks that the pendingAjaxCalls has a value of 0 before interacting with any page. I'm wondering, if a given page, were to import the js file multiple times; multiple
statements, how would that affect the value of my variable "var pendingAjaxCalls"?
The script would be run each time it was included, but make sure that you don't redefine the pendingAjaxCalls variable each time. i.e. Check it's defined before declaring it with something like:
if(!pendingAjaxCalls)
var pendingAjaxCalls=0;
/* code happens here */
pendingAjaxCalls++;
Each time you include a script using a script tag, the browser will download the file and evaluate it. Each time you include a JavaScript file the contents will be evaluated.
If the actual call to the function that increments your variable is being inserted more than once, you could be incrememting it multiple times.
On the other hand, if only the function that increments it is being inserted multiple times (not the function call), then JavaScript will use the most recently defined version of this function. So it shouldn't be incremented extra times in that case.
In other words, if you insert the same function twice but only call it once, you don't have to worry about "both copies" of the function being called, since one copy effectively overwrites the other.
Related
I have an electron project that is split into multiple windows, with several javascript files for each window. My problem is that there is no recognition that these files are linked by VS Code. For Example:
A variable declared in "mainwindow/constants.js" and then used in "mainwindow/socketFunction.js", will show up as greyed out and is declared but the value is never read. Likewise, if a function is declared in a certain file and used in another, ctrl-clicking the function name to go to function declaration does not work.
Is there any way to get VS code to recognize the structure of the files, ie: that variables declared in one file are the same that are being used in another.
EDIT: my file structure looks like this if it helps
I want to access a common variable from two differnrt jquery scripts (which will be loaded externally to the same page). so I use
window.commonVar = 0;
My question is what are the options I've got to update it? So that both scripts have the same last updated value?
So basically if script 1 do commonVar++ , script 2 should know the current value. same way around.
You have multiple options:
Declare the variable in which ever script you load first.
Declare the variable in your main html page in a script tag before you load your other scripts
Declare the variable in a third script that you load first before the other two scripts. This script could be composed of all gobal variables that you need access via other scripts.
You could even store it in a hidden input's value.
I have a log.js file(it contains log function along with some properties) for debugging purpose.
I have two other js file which are the controlling various behavior of the web application.
Now I need to include the log function considering not to repeat the debug function in both the js file and just calling the file name.
How do I do it?
The idea is to make my code clean and separate them in other files to limit the size of a single js file.
Include all .js-files in the html page. Include log.js first.
Call the functions all you want.
All functions in all files are included in the source and are "written on the page", any functions can be accessed from anywhere within the HTML as they all become essentially one document. Make sure you do not have duplicate functions as this could cause an issue
In my website we have one common javascript where we will assign value to a variable i want to call that variable in my aspx page as well as in another javascript file can anyone let me know how to declare that kind of varibale globally .
you can write a common javascript file which will contain all the global variable(assuming they are constant and they wont change).
if you have the variable value as non constant, there is noway that you can put it in a javascript file, instead you can use the local storage, cookies to store the information and use it across your javascript files.
the life time of a javascript variable is limited to the page, you navigate to other page its lost.
If you do not expect any page to assign new values in your common javascript, you can add it to the page as the first header script :
<script type="text/javascript" src="script/common.js"></script>
If you want to assign new values and reuse them, it is possible if you let ASP.Net generate your javascript file by passing a query string to the next page's URL, then in that page's script tag. Let's assume that your common variable is called "myGlobalVar". Supposing that "common_js.aspx" dynamically generates your variable definition, you can have something like this :
(URL : www.mysite.com/products.aspx?myGlobalVar=3)
<script type="text/javascript" src="script/common_js.aspx?myGlobalVar=3"></script>
This will keep track of your new js value whenever an update has to be made.
But if so, you may likely want to use a cleaner way to achieve what you want, for example : using the context manager instead of common js vars.
I am writing a widget template, which will be included in a page where it is installed.
One may install several of the same kind of widget in one page, so my template may get included several times.
Now I have written some JavaScript to initialize the widget, you know, the clickings and hoverings.
My problem is that these <script>s get executed multiple times, for example, when I click something, the bounded function get executed several times.
What is the best way to solve this problem?
EDIT:
By the way, I am using the Mako template engine, and I've tried using the c variable to store a boolean flag, but it seems that the c get overridden every time.
What about a similar solution to the one that's been used in C to prevent header-files to be included multiple times?
For example in the file "example.html" you could wrap your code in an if-statement like this:
<script type=text/javascript>
if (!window._EXAMPLE_HTML_) {
window._EXAMPLE_HTML_ = true;
// your code goes here
}
</script>