Js file inside I have a function with a particular algorithm.
For reading xml file and transform the data to variable name wordData.
Inside the function has the following line of code:
var wordData = xhr.responseXML.getElementsByTagName (Node1);
I can not set the variable "wordData as a global" outside the function or global
Inside the function
function language() {
lang = "heb";
if (lang == "heb") {
thisWord = wordArrayHeb[indeXML];
}
else {
thisWord = wordArrayEng[indeXML];
}
alert("language thisWord:=" + thisWord);
}
function setWord() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML) {
var wordData = xhr.responseXML.getElementsByTagName(Node1);
XMLength = wordData.length;
for (i = 0; i < XMLength; i++) {
wordArrayHeb[i] = wordData[i].getElementsByTagName(Node2)[0].firstChild.nodeValue;
wordArrayEng[i] = wordData[i].getElementsByTagName(Node3)[0].firstChild.nodeValue;
}
language();
}
}
}
}
the variable thisWord is effected from varible wordData which is not global.
Outside the functions, varible thisWord is empty
inside the function is ok and it has a value.
Would love help.
Thank you!
Simply declare
var wordData;
outside the function and change your line to:
wordData = xhr.responseXML.getElementsByTagName (Node1);
Hence removing the var declaration.
You can create a global var anywhere in JS by using the window object:
window['wordData'] = xhr.responseXML.getElementsByTagName(Node1);
or
window.wordData = xhr.responseXML.getElementsByTagName(Node1);
Then wordData can be accessed globally. This may not be the best solution for your problem, consider using function arguments and return values instead.
i think setting it as a global var is not the best choice, if you wish to access wordData inside the function language, you should pass it as a parameter like so:
language(wordData);
and in declaring function language(), just make it so it accepts the parameter:
function language(wordData) {
...
}
Related
I have a function in script1 :
function myFunction(arg){
--code JS--
};
In script2, i want override this function :
var func_name = "myFunction";
How can i do to obtain body of this function from function name in order to override it?
Thanks in advance
var func_name = myFunction; // without the quotes
I'm not sure what it is you are wanting to do. But you ask "How can i do to obtain body of this function from function name in order to override it?" and this is simply .toString() like myFunction.toString() This will return the entire function including the 'function() {}` outer block. To get the body, use a regex:
function.toString().replace(/^function\ (?:\w|\d)*\([^\)]*\)\ ?\{\ *|\ *\}$/g,"")
But to override the function you can just reassign it:
> function ab(){ return 1+2 }
< undefined
> ab()
< 3
> ab = function() { return 3+4 }
< function () { return 3+4 }
> ab()
< 7
If you want to use the same name but change the functionality, use anonymous function instead:
var myFunction = function() {
// initial code
}
Reassign new function to same variable:
myFunction = function() {
// new code
}
Call it like normal function: myFunction()
I'm trying to call a function that's returned from a function. Here's what I mean:
myFunction.something; // (Wrong)
function myFunction() {
return {
something: function() {
...
}
};
}
When I try calling myFunction.something nothing happens. How can I call a returned function outside of its function?
JSFiddle
var index = 0;
var animID = requestAnimationFrame(myFunction.something);
function myFunction() {
return {
something: function() {
index++;
console.log(index);
if (index === 5) cancelAnimationFrame(animID);
else animID = requestAnimationFrame(myFunction.something);
}
};
}
I would first of all recommend using descriptive variable names; utils rather than myFunction, and incrementFrame rather than something, for example. I would second of all recommend reconsidering your approach to code organization and simply putting all of your helper functions directly in an object, then referencing that object:
var index = 0;
var animID = requestAnimationFrame(utils.incrementFrame);
var utils = {
incrementFrame: function() {
index++;
console.log(index);
if (index === 5) cancelAnimationFrame(animID);
else animID = requestAnimationFrame(utils.incrementFrame);
}
}
There are a few differences between these approaches, some of them frustratingly subtle. The primary reason I recommend using an object for organization rather than a function which returns an object is because you don't need to use a function for organization; you are unnecessarily complicating your code.
myfunction is not the object that you get from calling myfunction(), it's the function itself and does not have a .something method.
You could call it again (as in myfunction().something()), but a better approach would be to store a reference to the object you've already created:
function myFunction() {
var index = 0;
var o = {
something: function() {
index++;
console.log(index);
if (index < 5) requestAnimationFrame(o.something);
// btw you don't need to cancel anything once you reach 5, it's enough to continue not
}
};
return o;
}
myFunction().something();
Alternatively you might want to drop the function altogether, or use the module pattern (with an IIFE), as you seem to use it like a singleton anyway.
Try this:
myFunction().something()
myFunction() calls the myFunction function
them we use the dot notation on the returned value (which is an object) to find the something member of it
that member is a function too, so add another set of brackets () to call it
Call function after writing it
var index = 0;
function myFunction() {
return {
something: function() {
index++;
console.log(index);
if (index === 5) cancelAnimationFrame(animID);
else animID = requestAnimationFrame(myFunction().something);
}
};
}
var animID = requestAnimationFrame(myFunction().something);
I have variable var valueRegionSelect = $selectOptions.eq($item.index())[0].value; in $.fn.pjSelect = function() {
I need this variable in another part:
/*******************************************************************************
* map
*******************************************************************************/
(function() {
var $mapForm = $('.b-map');
i think about use trigger, if i understand how it's work, but i guess i wrong:
if (valueRegionSelect !== "") {
$body.trigger('regionChangeOnSelect.pj');
}
Thanks for any help!
declare that variable globally
var valueRegionSelect = null;
//set the value in your function
valueRegionSelect = $selectOptions.eq($item.index())[0].value; //and use it anywhere
/*******************************************************************************
* map
*******************************************************************************/
(function() {
var $mapForm = $('.b-map');
if (valueRegionSelect !== "") {
$body.trigger('regionChangeOnSelect.pj');
}
Use this code
i want to get value from one function to the another function. and i want to pass the value to handler page. but in that i cant get the value to pass .. here i had given the code. please help me. for example assume that for radio=MR & fname=john.
function getdata()
{
alert('hi');
var radio = document.getElementsByName("radiobuttonlist1");
for (var i = 0; i < radio.length; i++)
{
if (radio[i].checked)
alert(radio[i].value);
}
var fname=document.getElementById("Firstnametxt").value;
alert(fname);
}
here i want to get all those values to another function this is my another function.
function sendinfo()
{
getdata();
$(document).ready(function(){
var url="Handler.ashx?radio="+radio+"&fname="+fname+""; "here i want the values from above function"
alert(url);
$.getJSON(url,function(json)
{
$.each(json,function(i,weed)
{
});
});
});
}
thank you . help me
You can do this two ways.
Option 1
You can define those variables outside of both functions like:
var radio;
var fname;
getdata() {
radio = document.getElementsByName("radiobuttonlist1");
fname=document.getElementById("Firstnametxt").value;
// VALUE UPDATED HERE
}
sendinfo() {
// VARIABLE ACCESSIBLE HERE
}
And then whenever the values of those variables is updated it will be updated at the scope those variables were initially defined (outside those functions).
The scope of a javascript variable is within the curly braces {} that variable is in and any curly braces within that set. Scope in javascript often refers to those curly braces {}. There are exceptions including the if statement in which you can define variables inside of and access outside (assuming the condition was true).
Option 2
Or you can pass those variables as parameters to the second function like: sendinfo(radio, fname);
Passing values by returning them
You can return the values as an object literal in your getdata() function like this:
getdata() {
return {
radio : document.getElementsByName("radiobuttonlist1"),
fname : document.getElementById("Firstnametxt").value
}
}
Then do this:
sendinfo() {
var data = getdata();
}
And then access those variables like: data.radio and data.fname.
You can declare two variables globally,set them to the values you need in the first function and access them in the second function.
var radio;
var fname;
function getdata()
{
alert('hi');
radio = document.getElementsByName("radiobuttonlist1");
for (var i = 0; i < radio.length; i++)
{
if (radio[i].checked)
alert(radio[i].value);
}
fname=document.getElementById("Firstnametxt").value;
alert(fname);
}
function sendinfo()
{
getdata();
$(document).ready(function(){
var url="Handler.ashx?radio="+radio+"&fname="+fname+""; "here i want the values from above function"
alert(url);
$.getJSON(url,function(json)
{
$.each(json,function(i,weed)
{
});
});
});
}
I have a scenario where i need to check whether a variable is not null using java script
Now these variables can be generated automatically and their naming convention is going to be like below
**Attribute.1.Name='aaa'
Attribute.2.Name='aaa'
Attribute.3.Name=''**
and so on.
how do i validate something like this where i do not now the exact variable name. All i know is the pattern of the variable.
Code example
FunctionName({'Attribute.1.Name':'test','Attribute.2.Name':'test2'});
Thanks
var FunctionName = function(parameters) {
if (parameters['Attribute.1.Name'] == null) {
...
}
}
and if you wanted to loop through all properties of the object:
var FunctionName = function(parameters) {
for (var name in parameters) {
if (parameters.hasOwnProperty(name)) {
if (parameters[name] == null) {
...
}
}
}
}