Cannot Access Global Variable Inside Object Declaration

May 9, 2011

Well, it turns out IE8 has yet another problem. My code has a global variable (to the object) inside an object declaration that cannot be accessed by a function (that is also global).

The code is like so:
Code:
function myobject(params){
//public
this.initialize = function(){...}
//private
some variables...
//problem variable
var mouse = new Object()
mouse.x = 0;
mouse.y = 0;

//code...
slidecontainer = document.createElement("div");
slidecontainer.onmousemove = function(event) {
if(boo.isIE) {
e = window.event;
mouse.x = e.x + document.body.scrollLeft;
mouse.y = e.y + document.body.scrollTop;
} else {
e = event;
mouse.x = e.clientX + document.body.scrollLeft;
mouse.y = e.clientY + document.body.scrollTop;}}

//this function is in an interval
function moveslides() {
/* this is where I have the problem

It seems that the function doesn't recognize that mouse.x, or mouse for that matter, exists. Mouse is global to the object, so why can't this function access it?
*/if(mouse.x) {
code...
}}}
The page is at [URL] and works on every browser, including finnicky opera, except for ie. The full code is availabe when you right click and view the source on the page.

View 3 Replies


ADVERTISEMENT

Variable Declaration Inside Eval Inside With

Apr 11, 2007

consider the next code:

var obj = {};
with(obj) {
var x = 10;
}
print(x);
print(obj.x);

It prints 10 and undefined. Here, one could expect that obj.x get the
value 10. But it's not the case, because variable declarations are
placed at the start of function code (or global code), so the
previous code is equivalent with:

var obj;
var x;
obj = {};
with(obj) {
x = 10;
}
print(x);
print(obj.x);

You can clearly see now that x is placed in the outer context. But
consider the next:

var obj = {};
with(obj) {
eval("var x = 10;");
}
print(x);
print(obj.x);

I was expecting that obj.x would get the value 10 here. But no, it
gives the same output as the previous code. I tested it with
spidermonkey, kjs and ie jscript. Looking at the ECMA spec, I could
not find anything that describes that behaviour. Code:

View 3 Replies View Related

Global Variable Glitch - Can Only Access The Variable On The Second Call

Dec 9, 2011

I am simply trying to use a global variable in javascript, but can only access the variable on the second call. I can't find anything that relates to this through my searches. My application is supposed to query the server for XML that tells me which years and months are available to put into combo boxes. I want to store this xml in a global variable to access it later.

[Code]....

View 6 Replies View Related

Not Able To Access Global Variable / Enable This?

Apr 3, 2010

I'm trying to populate a web page with javascript. I have 2 functions. One that runs on-load so I can populate a dropdown list. The second one to process the selection when the user selects an item from the list.

I have a global variable in the top of my javascript that I use in the functions and as long as I set it in it's global scope, everything works fine. But what I really want to do is to pass this into the javascript running on this page via the code...

My question is. Where do I put this code so that I can use it to set the global variable "weekNumber"?

I've tried putting this code in the populateList function - NOPE! I've tried referencing it via window.weekNumber and this.weekNumber - NOPE. I'm stumped.

I've spent far too many hours trying to understand this without success. Can anyone help me figure out where to put this code so I can set the global variable weekNumber?

Is there a better way to do this?

View 2 Replies View Related

Changing Global Variable Inside Function

Nov 26, 2010

im trying to change a variable set outside of a function and calling the function with an onchange... i'm having problems getting the variable to change

<script type="text/javascript">
var price = '<?php echo $price; ?>';
function addtwo()
{
if(document.add.size.value == "2xl")
{
price = price + 2;
}
}
</script>

View 4 Replies View Related

Make A Variable Inside A Function Global?

Mar 6, 2010

Is it possible to make a variable inside a function global (or at least usable in other functions)?

View 2 Replies View Related

Global Variable Inside A Document.getelementbyid.innerhtml?

Feb 5, 2010

I have a little JS app that is a glorified calculator which I posted the code for below. My code uses the document object to replace the html in the "content" <div> and works great.However, I want to add an inline style in order to change the background of the input (readonly field with an id of "coutput") based on either of the global variables named "MJPD" or "IJPD", (depending on the switch case selected in the user prompt at the beginning of the script.)Simplified....if the value of MJPD is less than 4.6, I want the "coutput" field's background to be red, else be green.The same goes for IJPD, except the threshold for red will be <3.83.Code and what I have tried is below.

<script language="JavaScript">
var MJPD = 1;
var IJPD = 1;

[code].....

View 1 Replies View Related

JQuery :: Can't Access Object Variable From Object Method

Mar 10, 2011

I am trying some simple things with javascript and trying to use it in a object oriented way. However I am now facing a problem in which I can't access an object variable from an object method. I am using jQuery.

My code is as follows;

Code:

My problem is that the variable msg1 does not work when accessed from function called from the jQuery get function. I get the message undefined. Therefore I am wondering, is there a way how I can access msg1 of my object instance from the get function of jQuery?

View 1 Replies View Related

Access/change Variable Inside Lambda Function?

Aug 17, 2010

In one of my functions I got the following lambda function. It works as intended, but I got a tiny problem. I'm trying to access the function-scooped variable "matched" and set it's value to "true". The problem is that it seems like the variable is existing as a local variable inside the lambda function, even if it's not declared inside the lambda function itself. So, the change "matched = true;" does not effect the variable that I declared.

Code:

//... code
var matched = new Boolean(false);
xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

[Code].....

View 3 Replies View Related

JQuery :: Access Variable Inside Callback Function Of Post?

May 4, 2010

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
var global_var = false;
function test_func() {
$.post("server.php", function(data){
alert("Data Loaded: " + data);

[Code]...

View 2 Replies View Related

Access Object Property With Variable?

Apr 18, 2010

I need to acsess an object property via variables, but don't get ahead.

Example:

var property = height;

"height" is a property of the object "flower". Now, I need a possibility to access "flower.height" with my variable property, means -->"flower.property" I tried everything like "flower.[property]" "flower.['property']" etc. but nothing did help.

View 2 Replies View Related

Get Variable Name Of An Object From Inside The Object

Apr 15, 2010

Is there any way to get the variable name of an object from inside the object?

[Code]...

View 18 Replies View Related

Store An Object Reference Inside A Variable?

Mar 9, 2011

How do I store an object reference inside a variable when I want the object reference to to reference the first "ul" html element nested inside the current object (as referenced by this keyword)?

var slideList = ???

I am trying to create a sliding menu.

View 5 Replies View Related

Object Declaration Question?

Oct 9, 2006

Below are some over simplified examples of code the create a single
instance of an object and I was hoping some one could give me the pros
and cons of each approach.

First declaration: In this case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.

ABC.Util.SomeObject =
{
get : function(val)
{
return this.__Private(val);
},

__Private : function(val)
{
return "testing";
}
}

Second declaration: In this case a single object is create and only the
get function is availabile for use and a closure is created.

ABC.Util.SomeObject = function()
{
var __Private : function(val)
{
return "testing";
}

return {
get : function(val)
{
return __Private(val);
}
};
}();

View 4 Replies View Related

Variable Declaration Issue

Jun 20, 2007

i did not undestand weather 'var' Keyword should be used or not when declaring variables. i already familier with php and there i will not use a 'var' for variable declaration. i like to program JS using similler PHP sysntax. so i code JS with out 'var' for varialb declarations and it works.

but can you please tell is it ok to declare variables with out the key word 'var'? if this is not good, then do all global and local variable declarations requir 'var'?

View 2 Replies View Related

Forms - Functions And Variable Declaration

May 28, 2009

I have been having trouble with forms and functions. On my wife's site I have some forms and some of them have radio buttons. My current radio button checker is cumbersome and it is time for something more elegant (some of you will say if it isn't broke don't fix it )

The new code is below:

Code:

Basically I want to pass into the function radio button values 1 & 2 denoted by firstChoice and secondChoice (eventually I want to also pass in the form name but 1 step at a time).

The buttons can have the value (names?) of pattern, chalkboard or kit. It is for a shopping cart (Mal's E-commerce) and this is part of the JS validation. I am using onsubmit to call the function viz.

HTML Code:

Seems ok (to me at least) but when I try to get it to work it throws up an error of

Code:

It stops at that point but undoubtably chalkboard would throw up the same error if it continued.

How would I define the variables in my function? Are they strings, integers, who cares?
Where would I define them? Global or local?

Is the problem a matter of syntax e.g. if I put ' ' or " " around them would that suffice?

View 10 Replies View Related

Initialize A Variable At The Point Of Declaration Or Not

Feb 8, 2009

I understand that there is no need to initialize a variable at the point of declaration. OTH, what is the default type of a [declared] variable? For example:

[Code]...

View 15 Replies View Related

Variable Declaration - Add Html Code As A Var Or A Const

Apr 9, 2011

I want to add this html code as a var or a const, so then later I could do document.write(x);

Like:

But this do not work.

View 3 Replies View Related

Variable Declaration - Specify Arguments When Writing A Function

May 11, 2011

If you specify arguments when writing a function should you still declare the argument variable inside the function?

example:

Code:

or

Code:

View 8 Replies View Related

Set Time As Variable Or Global Variable?

Sep 6, 2011

I am creating a survey. I want to set a variable, which is sent to google analytics, as the current time. this is the code and works fine:

HTML Code:
<script type="text/javascript">
<!--
var d1 = new Date();
var curr_date = d1.getDate();
var curr_month = d1.getMonth();

[Code]...

View 1 Replies View Related

Global Variable

Jul 20, 2005

If I have a global variable in one <SCRIPT>...</script>, is it available in the 2nd <SCRIPT>...</script>. I do not know if "global" means the whole document or the whole <SCRIPT>...</script> only.

View 3 Replies View Related

Global Object In WSH JScript?

Dec 18, 2008

What's the global object in a WSH JScript?

View 4 Replies View Related

Using A Global Function In A Object?

Dec 9, 2009

I have this code:

Code:

var Lightbox = function()
{
this.shine = function(a)

[code]...

I do not like this line: new Lightbox().shine(this). This way I will have [i+1] objects of the Lightbox. How can I change this code?

View 2 Replies View Related

Eliminating A Global Variable

Jul 23, 2005

I've created a site where the pages generally contain a table of
contents (site map) down the left side. When each page loads, the
first function is called. The second function populates a global var
(array) of all the links.

I try to avoid using global variables, but I am stumped how to
eliminate this one (TocLinks) because other functions need to iterate
through it. I think the answer is to create a custom object that the
other functions can access. But I need some pointers on how to
proceed.

Here's the function that the pages call initially and the 2nd function
that it calls:

View 1 Replies View Related

Global Variable Does Not Want To Change...

Nov 8, 2006

I have a global variable which does not want to change :
<header>
....
<script type="text/javascript" language="JavaScript">
var i=1;
function swap()
{
var window.i = 1 - window.i ;
}
.....
</script>
....
<body>

<A href="" ... onclick="swap()"<img src="pix/star.gif" </A>

when clicking on 'star.gif', i does not change from 1 to 0 ...

Any clue ?

View 4 Replies View Related

Global Variable In A Function

Jul 20, 2005

I have a .js file that receives a value from an html page. I'd like this
value to be a global variable that all functions in the .js file can use. I
have tried just declaring a var at the top of my .js file, but when the
value comes into the function and gets assigned to that variable, it is not
global.

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved