Can't Access Variable from other Javascript File - javascript

I am trying to access a variable from another file in JavaScript, and I'm not able to. When I try to print that variable out, for example, Intellisense suggests the variable. However, when I actually run it, I get the error Uncaught ReferenceError: myVariable is not defined
The variable is definitely declared in my file ('homePage.js').
I thought that all variables in Javascript were global, so I'm not sure why this is happening. All my files are in the same folder. Do I need to import something or am I just doing something completely wrong?
Thank you!

Maybe you are trying to access the variable value when your file still not loaded in the browser. You can check this using window.onload
window.onload = function() {
console.log('myVariable', myVariable);
// or execute some function that use the variable
}
https://developer.mozilla.org/es/docs/Web/API/GlobalEventHandlers/onload

You can also move variables into session state / cookie and pull it back into the other script from there.

Related

TypeError: this.ExportDataObject is not a function

I have a simple pdf file containing an embedded file (test.xml) I'm trying to add a JS to call it once the pdf file is opened (even with notification to user to accept the risk etc). I've read that to perform that, the JS that should be used is this:
this.ExportDataObject({cName:"test.xml", nLaunch:2});
For some reason, it is not working. I checked the debug js console on my Acrobat reader DC (version 2021.001.20145) the the error shown is TypeError: this.ExportDataObject is not a function. I'm not sure why on my "this" object the ExportDataObject is not available... I think it should be available always, shouldn't it? I also tested without the this. and the error is different ReferenceError: ExportDataObject is not defined.
That makes to think to me that this.ExportDataObject is existing but is not a function as the original error said... but, if is not a function, what is? a typeof is showing "undefined". Not sure how to make this work. Not sure if next steps should more JS debugging or if the problem is related to something on pdfs or Acrobat. Any help? thanks.
Javascript function names are case-sensitive and as documented by Adobe (p. 151), the correct spelling is exportDataObject() without the leading capitalization.
I believe you misspelled ExportDataObject()
It should be exportDataObject()
Using Javascript you should be careful as it is easy to mess up spelling as JS will interpret that in different ways.
As like most of the languages, js is also case sensitive.
But ReferenceError: ExportDataObject is not defined, ReferenceError always states that the object is not defined at all, and could'nt be found among the class methods.
so you need to make sure the function with the exact exportDataObject name is present and use them accordingly.

How to know all the JS file dependencies using JS code?

My main use-case is to know all the JS file dependencies!
If I have file a.js that define a function foo, and another file b.js that call the function foo, I want to know that b.js -> a.js where -> means that b.js depend on a.js.
Is it possible? Can someone think of a tricky solution for that?
I have tried to override the foo function and get the stack call, and it works fine, I know that b.js call foo, but I still do not know that foo was defined in a.js...
So maybe someone can think of a solution for this:
I have a file named "a.js" that define a global function foo.
And, I have a file named "b.js" that includes code that gets all the non-native functions on the window object, so here I got the global function foo.
Now, for each non-native global functions, I want to know the JS file name that defines them, so here for foo I want to get "a.js"
Is it possible? Can someone think of a tricky solution for that?
Note: I need to run it in the browser, Not using Node.js. And, not all the file are mine, so I can't edit them.
Run this code first, and check the call stack in debugger.
Object.defineProperty(window, 'foo', {
set: function() {
debugger;
}
});

Javascript console.log working with variable

Inside the console, you can simply declare variables and use them.
var x = 'thing';
undefined
x + 3;
'thing3'
I want set that variable from a Javascript file, and use it in the console. That does not seem possible, so I hope somebody knows a solution for it.
In a javascript file, typing console.log(var x = 'thing') gives compilation errors in Gulp. I know I can just type console.log(x), but I want to use the variable and play with it in the console.
How can I declare variables that I can use in the console?
You are looking for a simple
var x = {thing: 'thing'};
console.log(x);
The undefined in the console comes from evaluating a statement that doesn't return anything. If you don't evaluate it in the console, but put it into your js files, there won't be any log.
I'm trying to debug my webpage using console.log
Don't. Use a debugger. You can statically insert breakpoints in your code with a debugger; statement.
How can I interact with logged variables from a JS file in the console?
The best thing of breakpoints in a debugger is: you can interact with any variables in the scope of the breakpoint without even needing to log them.
Maybe use console.debug(myTest.test)
I will suggest you to use the debugger rather than using global variables as it's not secure if you are able to access those variables in console. Use chrome while debugging. Open console and select the js file which you want to debug and put breakpoints.

Lose my global variables in javascript using Yii

I am learning how to do server side scripting with the Yii framework. I am referencing a custom javascript file, however I am losing the global variable I created just for testing purposes saying x is not defined. When I tested it calling it directly through an HTML, I wasn't having this problem. Could someone please explain why I would lose the global variable using the Yii?
This is my testing code
var x = 4;
window.onload= function test(){
alert("This is the test function");
}
When I go to console after and type in x I get "variable is undefined". Curious as to why.
this has nothing to do with Yii or MVC.
I think that is a matter of scopes which your variable is defined, check these links out:
http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/
http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/
however without your full code it's very hard to determine why that's undefined!

jsHint "myFunction is defined but never used"

I am reviewing my javascript with JsHint, and it always returns an error, saying that a function is never used. Well, this is not entirely true.
I am integrating my javascript on the project index by external link (<script src="myJs.js"></script>), and on the index I call the functions I have on the myJs.js file after the actual file link. So it all looks like this:
<script src="myJs.js"></script>
<script>
myFunction();
</script>
The reason why I call my functions on the actual document and not in the myJs.js file is because I need some PHP functions running in it, and I need to call them from my index file.
The problem here is that JsHint returns a harmful, but annoying error, saying that "myFunction is defined but never used". Is it not aware that my function can later be called within my index? How can I solve this? Should I simply ignore it?
At the top of myJs.js you could try adding an exported declaration with your function name, something like this.
/* exported myFunction */
This should silence your warning.

Categories