Lose my global variables in javascript using Yii - javascript

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!

Related

How do I handle TypeError caused by Google Tag Manager (gtm.js)?

I have a website which uses Google Analytics and Google Tag Manager. It has been live for a couple of days but today the GTM script (gtm.js) started giving me errors in the console: "Uncaught TypeError: Assignment to constant variable". This, in turn, is causing some of my custom JS to not work as intended.
The GTM script is included as the second tag in as well as a immediately after the opening body tag, as instructed here : [https://developers.google.com/tag-platform/tag-manager/web][1]
This is the error that I get in the console:
Uncaught TypeError: Assignment to constant variable.
at <anonymous>:1:157
at <anonymous>:1:260
at gtm.js?id=GTM-XXX:271:414
at gtm.js?id=GTM-XXX:272:118
at b (gtm.js?id=GTM-XXX:272:337)
at dc (gtm.js?id=GTM-XXX:47:276)
at e (gtm.js?id=GTM-XXX:132:34)
at gtm.js?id=GTM-XXX:31:130
at Array.<anonymous> (gtm.js?id=GTM-XXX:133:454)
at Object.execute (gtm.js?id=GTM-XXX:133:194)
Does anyone recognize this issue? This error occurs in Chrome (Mac and Windows), Edge but not in Firefox (neither Mac nor Windows). Safari gives me a similar error "TypeError: Attempted to assign to readonly property."
First of all, you typically don't need a GA script if you have GTM. GTM is perfectly capable of managing GA.
Now to debug it properly, open the drawer in your devtools and go to the Network Request Blocker tool. Add a new blocking rule to block gtm. Reload the page, see if the error disappears. If it doesn't, then the issue is not in gtm. If it does disappear then we switch to gtm debugging.
Go to GTM, open site preview, get the error in the preview window, see what tags fired right before the error happened and try pausing the tags. See if by pausing them you can indicate which one causes the issue.
Or you could use a redirector extension (or a similar one) to replace your gtm container with a completely empty (but published) container and see if that error occurs. If it still occurs with an empty container, and you didn't make any trivial mistakes like rewriting a const in your CHTML tags/CJS vars/templates, it's likely your front-end interfering with GTM through some globals or overriding native functions, or, well, debug it further so we would know where to dig.
If you remove your custom JS tags, I suspect you'll see the error is resolved.
The error given is quite specific - there's an attempted assignment to a constant. I suspect this is happening within your own code as executed by GTM per the bottom of the stack trace you provided.
Put simply, somewhere in your code, I suspect you have something like the following:
const myVar = 123;
myVar = 456; //Error thrown here because you're illegally trying to change the assigned value
This fails because the const keyword indicates that the variable both cannot be redeclared nor reassigned in the same block scope.
It should instead read either of the following:
Using a 'var' statement
Var declares a variable named 'myVar' and assigns the value 'hello' to it. Other assignments can be made to the variable in the same scope.
var myVar = 'hello';
Using a 'let' statement
Let also declares a variable named 'myVar' and assigns a value of 'hello' to it. Other assignments can be made to this variable in the same block scope, but the variable itself cannot be redeclared.
//Simple example
let myVar = 0;
//Using this in a loop
for (let i = 0; i < 5; i++) { //Legal because you declare the variable once and can assign new values each iteration
//...
}
Swapping out the use of 'const' with any variable where you're assigning a new value to a variable and initially using the let keyword instead should resolve your error.

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.

Can't Access Variable from other Javascript File

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.

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.

how to check whether handlebar js is included in the application or not?

I am trying to check whether handlebar js is included in the application's HTML page or not. But it always returns "Uncaught Reference Error - Handlebars is not defined" even in the conditional statement. Guide me to achieve this requirement. Thanks in advance.
if(Handlebars){
// do something with the templates written
}else{
// ...
}
Trying to access an undefined variable on the global scope will always throw a ReferenceError, but there's a simple solution - just replace if (Handlebars) with if (window.Handlebars).

Categories