javascript global variables visibility - javascript

I'm using a global variable in javascript, declared in a script tag outside any function:
<script type="text/javascript">
var prov_status_dict={};
....
</script>
Later on in a javascript method I'm using the variable normally.
temp=prov_status_dict[current_as_id];
I'm having issues with it on opera and ie, while on firefox it works. This is what opera's error console reports:
JavaScript - http://localhost:8000/input/
Event thread: click
Error:
name: ReferenceError
message: Statement on line 62: Undefined variable: prov_status_dict
stacktrace: n/a; see opera:config#UserPrefs|Exceptions Have Stacktrace
I've noticed that the problem is with global variables in general. I tryed moving some into hidden fields, but the same error pops up on the next use of a global var.
Help?

I usually access my globals through the window object so that I always have a point of reference
window.MyVariables = {};
window.MyVariables.prov_status_dict = {};
Give this a try, it might resolve your problem.

Try to avoid using global variables, see http://yuiblog.com/blog/2006/06/01/global-domination

Related

Jquery code working fine until I included strict mode

A piece of Jquery Code was working just fine until "superiors" told me I must include strict mode on all my Javascript code. Everything is working fine on my main.js file except this piece of code. I can't reccon the error but my console error triggers every time i activate the script, it is a Boostrap 4 toggler button, it does open a menu, but the menu opens from up to bottom and really laggy. The button is supossed to collapse from right to left.
$(function(){
// mobile menu slide from the left
$('[data-toggle="collapse"]').on('click', function() {
$navMenuCont = $($(this).data('target'));
$navMenuCont.animate({'width':'toggle'}, 280);
});
})
Console error goes as follow:
Uncaught ReferenceError: $navMenuCont is not defined
at HTMLButtonElement.<anonymous> (main.js:61)
at HTMLButtonElement.dispatch (jquery-3.3.1.js:5183)
at HTMLButtonElement.elemData.handle (jquery-3.3.1.js:4991)
From the MDN reference:
First, strict mode makes it impossible to accidentally create global
variables. In normal JavaScript mistyping a variable in an assignment
creates a new property on the global object and continues to "work"
(although future failure is possible: likely, in modern JavaScript).
Assignments, which would accidentally create global variables, instead
throw an error in strict mode.
You need to declare your $navMenuCont variable.
For example:
var $navMenuCont = $($(this).data('target'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

IE6 Script Tag error

I've never seen this error. We have some clients that use Internet Explorer 6, so we need some pages to work on it.
We have the following HTML code:
<script type="text/javascript">
var bust = 236;
</script>
IE6 is throwing the following error message: 'Undefined' is null or not an object.
Do you have any idea what it might be?
Thanks.
It sounds like bust is conflicting with some other global. The global name space is really crowded.
You may be able to resolve it by wrapping your code in a scoping function so that it's not at global scope anymore:
<script type="text/javascript">
(function() {
var bust = 236;
})();
</script>
Or if it has to be a global, try another name until you find one that doesn't cause the problem. Global variables aren't fundamentally broken, even in IE6.

Can a global variable be made available only in the JS console?

Is it possible to have a global reference only be available from a JS
console, but not available to other JS running on a page?
For example, could I set a variable m to 10, so that...
<script>
console.log(m);
</script>
Is a ReferenceError, but from the console...
console.log(m);
Prints 10?
Edit:
The responses so far only touch on the basics of scope in JavaScript, but if this is possible, it's going to be way more hacky than adding a variable to a scope that only the console can see (yes, I know this doesn't exist). Here's an almost-working example of what I'm looking for:
Object.defineProperty(
window,
"m",
{
get : function window_m_getter() {
if (CONSOLE_OPEN) {
return 10;
} else {
throw new ReferenceError('m can only be used in a console');
}
},
}
);
This example would work if it were possible to detect whether the console was currently open, which it really isn't, but it's pretty close.
The answer to this question is almost certainly no, but it isn't entirely because of scope.
No.
The developer console operates in the global namespace. It's the same one every script uses, hence the name. There is no private, different namespace for the console.
The very console object you are accessing to log() is part of this namespace, and thus available to everyone.
At most, you can declare global variables for the console after you've loaded all of your scripts, at the very end of the <body>, making them unavailable until after everything runs. This, however, won't stop scripts from accessing them afterwards, from triggered events.

Undefined jscript in javascript

I have created an ASP.Net application. I am using a javascript which is written in a separate file. I am using Var myvariableName ={} in javascript file.
I have included this file in MasterPage and accessing myvariableName in my aspx page.
This is working fine in Google Chrome, however, in IE 8 an unhandled exception is thrown as
myvariableName is undefined.
the error shows as;
0x800a1391 - Microsoft JScript runtime error: 'Common' is undefined
where Common is my javascript variable.
Please assist me in resolving this issue.
You're probably accessing the variable before your external script is executed.
Be sure to access your variable as soon as the document is fully loaded (i.e. $(document).ready(function(){...}); if you use jQuery) or try to find out the real execution order with some alert (that shouldn't be browser-depentent, by the way!).
If your code is already in a document.load or $(document).ready(function) you can always
handle the variable before acessing it via
if (typeof myvariableName !== "undefined") {
// do stuff
}
Some times in IE shit happens and window.load gets screwed specially when async calls are in place.
You are probably getting this error due to a missing semicolon. Change the code to this:
var myvariableName = {};
In your asp page, where you access your custom variable, wrap your code with:
var myvariableName = {};
window.onload = function(){
// your code here where you're accessing the variable
};

Safari generates "Can't find variable" error although it is already defined in another Javascript page

The JavaScript script works with Chrome and Firefox but not in Safari. The code is:
$(document).ready(function(){
$(window).load(function() {
myVariable.start();
});
This generates:
ReferenceError: can't find variable: myVariable
The variable is defined in another JavaScript page that is included in this page, but for some reason Safari doesn't see the definition in the other page. Is Safari executing this script without loading the page that the variable is defined in?
How can I fix this?
Thanks for any help
Try to avoid setting global variables.
Maybe try assigning your variable to the window object, on top of the page:
window.myVariable = { start: function() {} };
Then when you need it:
$(window).load(function() {
window.myVariable.start();
});

Categories

Resources