user1596537
user1596537

Reputation: 7

How do I get different tabs to load separate javascript files?

My code is as follows:

<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Demo</title>
    <link href="assets/bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
    </head>
    <body>

        <div class="tabbable">
            <ul class="nav nav-tabs">
                <li class="active"><a class="atab" href="#a_tab" data-toggle="tab">A</a></li>
                <li><a class="btab" href="#b_tab" data-toggle="tab">B</a></li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="a_tab">
                    <h1>A</h1>
                    <acontent></acontent>
                </div>
                <div class="tab-pane" id="b_tab">
                    <h1>B</h1>
                    <bcontent></bcontent>
                </div>
            </div>
        </div>

        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://d3js.org/d3.v3.js"></script>
        <script src="assets/bootstrap/js/bootstrap.js"></script>

        <script>
            $(".atab").click(function() {
                $.getScript("assets/a.js");
            })
        </script>
        <script>
            $(".btab").click(function() {
                $.getScript("assets/b.js");
            })
        </script>
    </body>
</html>

What I'd like to do is when I switch tabs, load a different chart using the D3.js libraries.

a.js and b.js are essentially the same, except where I select the particular element ("acontent" or "bcontent"):

var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;

var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var line = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.val); });

var svg = d3.select("acontent").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv("data.tsv", function(error, data) {
  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.val = +d.val;
  });

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return d.val; }));

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Price ($)");

  svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);
}); 

The problem is, I can't seem to isolate the two scripts. Once I load up A, when I switch to B, I get the same graph as I did with A. Is there a way for me to set it up in such a way that I can have two different graphs show up under two different tabs?

Upvotes: 0

Views: 3526

Answers (1)

widged
widged

Reputation: 2779

The code shown seems to work when the code for a.js and b.js are encapsulated within a self-executing function to avoid possible variable name conflict.

!(function (d3) {
   ... a.js code shown above ...
})(d3);

Demo and code: http://bl.ocks.org/4561185

Upvotes: 1

Related Questions