When debugging js in browser, usually you put a breakpoint, and when the code hits there, you can access all local variables no matter your mouse hovers on it or you just type the variable name from the console (because you are in the execution scope)
But it seems to behave differently in react since I am able to see all local variables. However I cannot access them from the console or change its value from the console. Refer to the screenshot below.
So I can access url but cannot access response or timeTakenByFetchApi
My understanding is that console.log will be executed straight away, even though the promise has not returned anything.
Related
GTM is returning 'Undefined' when I use the DataLayer Variable call in my JS.
In the console log of the page I type, google_tag_manager['GTM-XXX'].dataLayer.get('ecommerce.transaction_id') and it returns the correct value.
When I use dataLayer[5].ecommerce.transaction_id it also returns the correct value.
Via GTM using {{dlv - ecommerce.transaction.id }} returns 'undefined'. I added a console log to see the output for debug reasons.
Here is what the GTM Variable Config looks like:
Don't use console logs to debug GTM. Use GTM preview.
In GTM preview. select the event at which you expect your variable to appear, go to variables and see its value. It's supposed to be undefined there. Now go to your datalayer and see what's there. The transaction is likely to not be there at the moment of your query. Go through events, see where the dataLayer event push happens. That's where you're supposed to access your variable.
If not, add screenshots from the preview.
Also you should indicate whether this is GA4 EEC DL structure.
I've looked at several questions asking how to remember the variable, but I'm having the opposite problem which is surprising to me.
My main.js file will have this:
console.log(name);
and I get nothing. If I try to log it in the console it returns undefined, that is expected,
now I create the variable like this:
var name = "Sandy"; //global variable
console.log(name); //returns "Sandy" which is also expected.
But now the unexpected happens. I remove the variable so we're back to this, and then I refresh the page:
console.log(name); //This returns "Sandy" still...
How is this happening and why? I thought it was cookies, so I tried it in incognito mode (Maybe I misunderstand incognito?) But it works exactly the same.
In order to make the variable go away I have to close down the browser and open up a new window.
After reading briefly about LocalStorage, cookies, and incognito, it sounds like cookies are the problem, but wouldn't I have to create the cookie manually?
It seems like the browsers should be forgetting the variables unless I explicitly set the variable to a cookie.
By declaring a global variable called name you are overwriting a window.name property, which doesn't reset on page refresh.
window is a built-in object, specific to each opened tab in a web browser and represents a containing document. You can read more about the window object here and more about its name property here.
You can also check what happens during execution of your code by logging window.name before and after you define your variable.
It's best to avoid using name as a variable in JavaScript code that runs in a browser. Set your variable to something else (that is not a reserved word or a propery name of a built-in object) and your code will work.
I have a variable in JavaScript:
var userIp = '192.168.0.1';
However, user can open browser console and overwrite it:
userIp = '123.45.127.21';
How I can lock this variable, to user can't change it value? Is it possible?
How I can lock this variable, to user can't change it value? Is it possible?
No, it isn't. You can make it harder by making the variable not a global, but it's still not remotely hard.
Client-side code is completely and totally insecure. Users can change values of variables, modify code, completely replace the code, etc. They can also manipulate the page contents. Anything the client side sends to the server may be spoofed, and so the server has to treat everything it receives as potentially-compromised.
Let's say I make a call to local storage like so:
window.localStorage.setItem("key", bigJsonObject);
And immediately afterwards, the user closes their web browser. What will be the result of
window.localStorage.getItem("key")
Will the bigJsonObject be partially written? Or will the whole write fail? Is their any way to guarantee that there will be no partial writes?
Refer to §4.1 of the "web storage" specification:
The setItem() and removeItem() methods must be atomic with respect to failure. In the case of failure, the method does nothing. That is, changes to the data storage area must either be successful, or the data storage area must not be changed at all.
However, there have (historically) been browser bugs in this regard, e.g. some time before Chrome 21 until some time before Chrome 29.
I just began to learn javascript, so here is a silly question :
What happens to a javascript variable after a call to the server? Are all the variables wiped out?
I read somewhere that javascript variable in ajax can act like session or cookie. Is that true?
All of the run-time state is reset whenever the browser does a page-load, such as navigating from foo.com/bar to foo.com/baz. This includes all JavaScript variables, as well as the current DOM. However, asynchronous calls to the server, such as XHR, do not affect run-time state, and all JavaScript variables will stay.
If you'd like to preserve values between page-loads, you can use cookies or localStorage.
It depends, what scope the variable is in. Also, Ajax is different then submitting a page, so your variables are persisted.