1

I have java script file that reads in a csv file, and has many functions to to visualize the data differently, using d3. I want each visualization to be displayed in different html pages. Ive made every html page, refer to the javascript file. Each page also has its own script that calls functions from the referred javascript file.

The problem, I am facing is that when ever I go to a new page, the function gets called before the data being read. Only works when refreshing the page. I there a way to just read the data in once, and every page can access it.

If you look at the code below, callingFunction() returns undefined (maybe because data is not stored in var data?) The callingFunction() works fine when the page is refreshed.

 <!DOCTYPE html>
    <html>

    <head>
        <meta charset="UTF-8">
        <title>D3</title>

        **<script src="my.js"></script>** //my script file

    </head>

    <body>
        <script>
         callFunction(); //uses var data defined in my.js
        </script>

Here is the "my.js":

var data;
function data(file){d3.csv("file", function (error, data) {
            ds = data;
            callFunction();
}
3
  • You could save the data in localStorage. But why do you separate pages? Why not use a single page, and have a menu that lets the user select which format to display it in? Commented Jun 3, 2014 at 3:16
  • 1
    Welcome to Stack Overflow! Please read how to ask a good question and also what have you tried. Commented Jun 3, 2014 at 3:35
  • Hope question makes more sense now Commented Jun 3, 2014 at 4:42

1 Answer 1

1
  1. load your script after the target DOM :

    <canvas></canvas> ........................... <script type="text/javascript"></script>

  2. Use angularJs Js library: it organize your js code according to MVC design pattern. enter image description here

  3. If you have more than one method to implement a business logic , Use console.time & console.timeEnd to evaluate code performance .

    console.time('anID');
     // Here your code
    console.timeEnd('anID');
    
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain more about your first suggestion. I am referring my script just before the closing <head> tag.

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.