Variables Scope In Dynamic Functions - Display The Number 1 At First And Then 2

Mar 19, 2011

I have got this piece of code:

Code:

I would like to display the number 1 at first and then 2. but this code produces number 2 for both alerts. I was able to achieve what i wanted with "new" constructor when creating functions but this is not a good practice and after all i am passing these functions as an event handlers and it can't be done with "new" keyword because it is throwing error. I think there are some solutions with arrays e.g the x would be an array of numbers, but i don't like it. Am i missing something important?

View 3 Replies


ADVERTISEMENT

Scope Of Variables When Adding Functions With Prototype?

Jul 12, 2009

I'm trying to learn more about creating and using JavaScript objects and ran into this situation the other day:

Code:
function someObject() {
var size = "large";

[code]....

View 5 Replies View Related

Objects, Callback Functions And Variable Scope

Nov 23, 2005

I am trying to convert some of my javascripts into a class and am
running into difficulties which i think are related to variable scope.

Basically I have a constructor function for a calendarInput class that
takes 4 parameters, the first is a reference name/number for this
input. I also have some functions for importing my PHP classes into
Javascript using AJAX (properties only. still trying to get methods
working but that's another story!). The relevant function is called
call_object_method(classname, methodname, createparams, methodparams,
post, callbackfunction). This creates an instance of the specified PHP
class using the parameters in createparams. It then calls the
specified method using the parameters in methodparams. The result is
passed back to the javascript function specified in the callbackfunction parameter (ie the value of xmlhttp.onreadystatechange is set to callbackfunction before xmlhttp.send() is called)

The function i am trying to fix is called show (x,y) which creates the
html for the calendarInput and displays it at co-ordinates x, y.

So, my code is as follows:

function calendarInput(calendarid, target, displaytarget, value)
{
this.calendarid = calendarid;
this.target = target;
this.displaytarget = displaytarget;
this.value = value;

this.show = function(x, y)
{
<!--// bunch of init and prepping code. includes creating
createparams and methodparams arrays //-->

call_object_method("cms_calendar", "getcalendarview",
createparams, methodparams, false, this.showcallback);

}

this.showcallback = function()
{
alert(this);
<!--//code to create html//-->
}

}

I know i've cut out most of the innards of this. This is because I
have already tested these functions and had the calendarInput working
outside of a class, hence am pretty sure that this is ok (plus it runs
to almost 1000 lines these days!!). My problem is that when I call the
show method, the alert on the first line of the callback function
returns the function showcallback instead of (as i was expecting) the
instance of the calendarInput object. Whilst this kinda makes sense I
can't figure out how to reference the Object instead. I have tried
'this.parent' but this returned undefined. I have tried changing the
way i reference the callback function (ie the final parameter of
call_object_method) but no joy.

View 2 Replies View Related

JQuery :: Variable Scope And Un-named Functions?

Aug 23, 2010

I've recently started developing javascript using jQuery and so far I'm really enjoying it.Being a professional programmer (mainly C, C++, java and python) there is one thing that's been puzzling me regarding variable scope and unnamed function declarations. Let me exemplify:

------------------------
var sum = 0;
$(xmlobj).find("item").each(function(){

[code]....

View 6 Replies View Related

JQuery :: Pass Global Variables To The Scope?

Jul 19, 2010

I'm having trouble letting functions I defined in jquery read javascript global variables I defined elsewhere.. for example I have a function defined within my jquery

$(document).ready(function(){
function performTabSlide() {
// do something with currentPosition;

[code].....

View 3 Replies View Related

Share Variables Between InnerFunction And Closure Scope?

Sep 22, 2010

I had a original function in this format/syntax:

Code:
GlobalFunction(){
InnerFunction(){
// ... long definition here ...

[Code]....

Other words the innerFunction behaves like it is global and has not shared variables with myClosure!

View 14 Replies View Related

JQuery :: GetJSON Method - Scope Of Variables

Sep 16, 2011

I'm using jQuery's getJSON method to get the current date from a foreign server. I however, should this request fail (e.g. if the server is down), I want to have my page fall back to a sensible default.

What I'd like to do is this:
var date = "sensible default"
$.getJSON(url, function(data){ date = new Date(data.datetime); });
if request was successful date will contain new Date(data.datetime);
if request was unsuccessful date still contains "sensible_default"

What I have is this:
var timezone = "Europe/Berlin";
var date = "sensible_default";
$.getJSON("[URL]"+timezone+"&callback=?",
function(data){ date = new Date(data.datetime); });
alert(date);
But this produces an alert of "sensible_default", despite the fact that the getJSON request fired correctly.

View 4 Replies View Related

Move Global Variables To Prototype Scope?

Jul 14, 2010

I'd like to move the global variables vertexBlaBuffer to the scope of the Geometry prototype [code]...

View 1 Replies View Related

Variable Scope - Anonymous Functions And Self Invoking Closures

Jul 25, 2011

I think the last thing people seem to learn about in JavaScript when they're not coming from other programming languages is variable scope. Some even get all the way into AJAX without having learned about scope, and this is a time when it's really needed. Although the scope of JavaScript variables is non-complex by nature, it's something we should all get a full understanding for before we move too far.
Section 1: What is "scope"?
Section 2: The "var" keyword
Section 3: The "this" keyword
Section 4: Closures or "Anonymous functions and self-invoking closures

View 5 Replies View Related

Variable Scope And Crossing Functions - Resize Plugin In Order To Obtain The Varying Computed Width Of An Element When The Window Is Resized

Mar 23, 2010

I am using Ben Alman's JQuery resize plugin in order to obtain the varying computed width of an element when the window is resized (the element in question is a page wrapper that exhibits the expand-to-fit behavior of a block box, and it's computed width is obviously influenced by the resizing of the window. Essentially, what I need to be able to do, is to reference a variable that is defined in a .resize() function ('width_page') in a seperate .each() function.

[Code]...

I now understand that variables can't cross boundaries like in the example above, which leaves me a little stuck. I also understand that this is specific to the context of the .resize() function, and that it can't be taken out of it without using an element selector. Is there some way I can call the .resize() function in my .each() function?

View 3 Replies View Related

Does Variables Value Become Available To Other Functions Within The Document?

May 17, 2009

When assigning a value to a variable within a function, does that variables value become available to other functions within the document?

View 2 Replies View Related

Keeping Variables Between Functions ?

Dec 13, 2009

I've noticed that if you declare a variable in one function, then call another function, the variables cannot be accessed in that function. Is there any way to get the value of a variable declared in a different function?

View 5 Replies View Related

Passing Variables In And Out Of Functions

Nov 3, 2010

To formulate the problem simple i have two buttons with the onclick function:

I guess what im asking is how can you set a variable by one onclick function and then use it in any other function?

View 1 Replies View Related

Pass Two Php Variables Into My JS Functions?

May 2, 2009

i just want to pass two php variables into my JS functions. but it seems its passing one value and another is showing undefine.

HTML Code:
<input text="text" name="friends1" value="" id= "friends1"/><span><input type="hidden" name="id" value="<?=$row['id'];?>" /><a href="javascript:edit(<?php echo $row['id'];?>,friends1);"></span>Edit</a>
edit.js[code]...

i am getting friends value but not getting id's value.how can i pass these two values?

View 1 Replies View Related

Access Parent Functions Variables?

Jul 5, 2010

Code...

I am trying to create a simple class in which i want to sent the id of a div to fade in and fade out...

so that i can dynamically change its color width and height with a basic class.

I read about closures but inside jquery this is referring the div notex....

how to access parent functions variables?

View 2 Replies View Related

Passing Variables To Multiple Functions?

Sep 5, 2010

I'm new here, and new to js. Here is my problem: I have written out a code to make an image switch from state 0 to 1 and back to 0 again (an eye blink). The code works fine, but I would like to write the functions with arguments so it could be applied to more images. I have tried for a few hours (and searched forums) and am getting no where. Here's my code.

Code:
function home_blinkDown()
{
//alert('blink down');
var t = setTimeout("home_blinkSwap('home_js', 'images/main/home_blink.png')", 2000);

[Code]...

View 5 Replies View Related

JQuery :: How To Write Global Variables In Functions

Nov 26, 2010

I cannot findout how I can write to global variables in a a JQUERY function.
<script type="text/javascript">
function checkusername (username ) {
$.post("[URL]", {
action: 5, username: username },
function(xml) {
result = $("apot", xml).text();
//<-- this variable is not global... why ??
message = $("message", xml).text();
//<-- this variable is not global... why ??
alert(message); //<--here variables are shown ok
alert(result);
});
alert(message);
//<--here variable is NULL !!
alert(result);
//<--here variable is NULL !!
}
</sctipt>

View 1 Replies View Related

Passing Values Of Variables Between Functions Not Working ?

Aug 12, 2010

I'm trying to trap a user entry that cannot be found in the database. When a code is entered, the page should give a "Code Not Found" message if it is an undefined value.

However, it didn't; and it always shows the "undefined" value to the page instead.

Here's what I actually did:

getting user input:

Finding the code:

View 1 Replies View Related

Adding Two Number Variables?

Mar 15, 2009

Im having truble with this codein alert(q+w) if q is 3, and w is 4 it will display 34 not 7and if e==false it still does if r==true

[code]
var q=prompt("Type your first number");
var w=prompt("Type your second number");

[code]....

View 1 Replies View Related

JQuery :: Passing Local Variables To Callback Functions?

Jun 2, 2011

for some time I always found a workaround for this kind of problem. But somwhow, it feels wrong. So I'd like to know, if there's a common way to solve something like this[code]...

I'm defining the function and if the value of 'obj' hasn't until the function really gets called, this can work. But it just feels so wrong do have always global variables just to access them inside of a callback.[code]...

View 4 Replies View Related

JQuery :: Functions With Variable Number Of Arguments?

Jul 26, 2011

So in the Tutorials:How jQuery Works, I've noticed that you can make a call to .get like this : $.get('myhtmlpage.html', myCallBack);

I've also noticed that in the docs jQuery.get() is described as jQuery.get( url, [data,] [success(data, textStatus, jqXHR),] [dataType] ). My question is, in the above example, the callback function is the second argument, whereas in the docs the second argument is data, and the third is the callback function. How does this work? Also, what does it mean when the arguments are laid out in an array-like fashion with commas in the square brackets? i.e. arg1, [arg2,]...

View 4 Replies View Related

Dynamic Variables

Jul 23, 2005

I need to create a variable out of nothing. From a database I extract an
item with a certain id. With this id I want to create a new variable.
For example:

id = 36;

"item"+id = new Array();

Now I get the message "Illegal left hand assignment".

I tried:

eval("item"+id) = new Array();

Is it possible to create a variable out of nothing?

View 4 Replies View Related

Limit On Number Or Size Of Javascript Variables

Jul 20, 2005

There will be a number of list boxes and other controls, with pop-up windows
to edit certain properties. It's the kind of thing I would normally have
done in VB but I want it to be browser-based. I've only used javascript for
trivial things before so this would be my first serious javascript
development. I would like it to run on all reasonably recent browsers.

The form starts with all the initial values being received from the server
(presumably just by pre-initialised data structures). The user tinkers with
it and when he is happy he presses 'submit' and the whole lot is submitted
to the server (presumably as a form post). This would be a few kb of data,
possibly 100 individual values but obviously in various data structures. I
guess there would be a few hundred lines of javascript code to manipulate
it.

My question is, is there likely to be a problem with manipulating and
sending this amount of data in Javascript. Sorry if this is a dumb question,
but like I said, I only used javascript for tiny programs before, so I'm a
bit unsure about its capabilities.

View 2 Replies View Related

Dynamic Variables In AttachEvent

Jan 24, 2007

I am trying to write a loop that will add 10 divs to the screen. Each
div will have an onclick event. The function that will be called
onclick requires a parameter. That parameter is dynamic based on the
index of the loop. In Firefox this is no problem. However in IE I get
some results that I wouldn't expect.

Here is my code:

for(var i = 0; i < 10; i++)
{
var linename = jsonObj.lines[i].line;
var childcountid = jsonObj.lines[i].childcount;
var lineid = jsonObj.lines[i].lineid;

var newdiv = document.createElement('div');
newdiv.setAttribute("id","main" + i);
if (navigator.appName == "Microsoft Internet Explorer")
{
//************ this is the problem area
*****************
newspan.attachEvent("onclick", function() {getCategories('main' +
i)});
}
else
{
newspan.setAttribute("onclick", "getCategories('main" + i + "')");
}
document.getElementById('container').appendChild(n ewdiv);
}

What happens is when the element is clicked the parameter being passed
to getCategories is always 'main9' IE always grabs the current value
of i, not the value of i at the stage of the loop that attachEvent was
called.....

View 1 Replies View Related

Passing Dynamic PHP Variables?

Jul 16, 2011

I'm creating dynamic buttons and forms for a website and would like the form hidden when it's corresponding button is clicked. However, when I pass button and form name variables to my JavaScript that hides the form, the variables are not being recognised unless I explicitly set them. (e.g 1 and 2 as seen below).

In a nutshell the onlick event doesn't seem to like PHP variables !

[Code]...

View 1 Replies View Related

Dynamic Named Variables

Feb 18, 2004

I have a little problem that doesn't seem to be working. I'm trying to access a variable using its string name which is passed when the function is called Code: ie:

function a(varName, other crap...) {

document.getElementById("sometag").innerHTML = ['varName'].somevalue;
}

// I would call this function like so...
a("temp1", othercrap...);

The variable temp1 is set in another script file. I don't specifically know which variable I will be passing to the function a(), all I have is its string representation. I can access the values of temp1 if I specifically call it but I can't seem to figure out how to call it dynamically.

View 2 Replies View Related







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