0

I have a .js file which looks something like this:

function regionmap () { 
var width = 960,
    height = 500,
    centered;

var projection = d3.geo.albersUsa()
    .scale(width)
    .translate([width / 2, height / 2]);

var path = d3.geo.path()
    .projection(projection);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .attr("id","svg");

var states = svg.append("g")
    .attr("id", "states")



    d3.json("readme.json", function(json) {
      d3.select("svg").selectAll("path")
          .data(json.features)
          .enter()
          .append("path")
          .attr("d", path)
          .on("click", clicked)
          .append("title") 
          .text(function(d) { return d.properties.name; });
});


listofnames = new Array();
listofnames.push("Regions:");

function clicked (d) { 

    var regionname = d.properties.name;

    var currentclass = d3.select(this).attr("id") ;

    if (currentclass == "active") {
        d3.select(this).attr("id", "nonactive");
    } else {
        d3.select(this).attr("id", "active");
    }
    var contains;
    var index;
    for (var i = 0; i < listofnames.length; i++) {
        if (regionname != listofnames[i]) {
            contains = false;
        } else {
            contains = true;
            index = i;
            break;
        }
    }

    if (contains == false){
        listofnames.push(regionname);
    } else if(contains == true){

        listofnames.splice(index,1);
    }

    var x=document.getElementById("demo");
    x.innerHTML=listofnames;    
}


    function sendingvariable (){
        window.location.href = "../php/fileregions.php?name=" + listofnames; 
    }


}

The thing is that when calling the function from html I first call the function regionmap on click ( onclick="regionmap()) which works good. However, I then need to call the function sendingvariable from the html, and I am not able. Any way to solve this?

4
  • Show us a little more code please? which variables? (because right now my solution is, remove the } from line 4 and move it to the end of line 1. Then function a and b both work. Commented Jun 26, 2013 at 13:35
  • Study up on the Module pattern here: adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html Commented Jun 26, 2013 at 13:36
  • Then just declare it as window.b || (window.b = function () {}); Commented Jun 26, 2013 at 13:37
  • This is just the way how the javascript scope works Commented Jun 26, 2013 at 13:37

4 Answers 4

1

You can make it visible for the outer scope using a variation of the modular pattern:

function global() {
    function a() {};
    function b() {};

    // public api
    return {
        b: b
    }
}

var glob = global();
glob.b();

Now internal b function is globally accessible, while a is still private.

Sign up to request clarification or add additional context in comments.

Comments

0

The functions a and b are hidden from anything outside the global function scope (called a closure). You can return something from the global function (an object) that contains the a and b functions. You can then call these functions from there:

function global(){
    return{
        a: function(){
           // do something on a
        },
        b: function(){
           // do something on b
        }
    }
}

// Call like so:
global().a();

2 Comments

It works but I still have a problem. I just did the return for function b, in function b I use a variable declared in global but updated in a. The thing is that when using b I need that variable which has been updated from a. Like I did it, it doesn't get the updated variable. Any idea to solve this?
If that is what you want you need to create an instance of the global function (which will become an object on itself). var myGlobal = new global(); myGlobal.a();
0

Try to add window.b = b after function b().

Comments

0

If you need to call a() without anything from global() local variables you can just declare it as separate function. If you need OOP model you should create object from global and then call a(). Also declaration have to look:

function global() {
    var a = function() { ... }
}

And then you can call it like this:

var obj = new global();
obj.a();

Sorry if I didn't understand your question right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.