简体   繁体   中英

How to run javascript function from $(document).ready()

I'm using jsgantt library to create a Gantt chart on my web page. The problem is the following.

Variant 1 is not working , while Variant 2 is working . The alert "Start" is displayed, while alert "Fin" is not displayed inside the function createChartControl . So, I cannot figure out why Variant 1 is not working (ie blank DIV container) and Firebug does not display any error message. Any help is highly appreciated.

Variant 1:

    <script>
    function createChartControl(htmlDiv)
    {

alert("Start");

          var g = new JSGantt.GanttChart('g',document.getElementById(htmlDiv), 'hour');

          g.setShowRes(1); // Show/Hide Responsible (0/1)
          g.setShowDur(1); // Show/Hide Duration (0/1)
          g.setShowComp(1); // Show/Hide % Complete(0/1)
          g.setCaptionType('Resource');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)
          g.setFormatArr("hours","minutes")

          if( g ) {

            // Parameters             (pID, pName,                  pStart,      pEnd,        pColor,   pLink,          pMile, pRes,  pComp, pGroup, pParent, pOpen)

            // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

            g.AddTaskItem(new JSGantt.TaskItem(1,   'Add minutes/hours',            '',                 '',                 'ff0000', 'http://help.com',        0, 'Ilan',     0, 1, 0, 1));
            g.AddTaskItem(new JSGantt.TaskItem(11,  'Add support for half days',    '5/14/2009 14:00',  '5/14/2009 15:30',  'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan',  100, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(12,  'Add minute view',              '5/14/2009 16:00',  '5/14/2009 17:00',  '00ff00', '',                       0, 'Ilan',   40, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(13,  'Add hours view',               '5/14/2009 16:00',  '5/14/2009 17:00',  '00ffff', 'http://www.yahoo.com',   0, 'Ilan', 60, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(14,  'Add support for format options',               '5/14/2009 18:00',  '5/14/2009 19:00',  '00ffff', 'http://www.yahoo.com',   0, 'Shlomy', 60, 0, 14, 1));


            g.Draw();   
            g.DrawDependencies();

alert("Fin");

          }
          else
          {
            alert("not defined");
          }
    }
    </script>

    <script>
    $(document).ready(function() {
                               createChartControl('schedule');

    });
    </script>

    <div style="position:relative" class="gantt" id="schedule"></div>

Variant 2:

<div style="position:relative" class="gantt" id="schedule"></div>
<script>
      var g = new JSGantt.GanttChart('g',document.getElementById('schedule'), 'hour');

      g.setShowRes(1); // Show/Hide Responsible (0/1)
      g.setShowDur(1); // Show/Hide Duration (0/1)
      g.setShowComp(1); // Show/Hide % Complete(0/1)
      g.setCaptionType('Resource');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)
      g.setFormatArr("hours","minutes")

      if( g ) {

        // Parameters             (pID, pName,                  pStart,      pEnd,        pColor,   pLink,          pMile, pRes,  pComp, pGroup, pParent, pOpen)

        // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

        g.AddTaskItem(new JSGantt.TaskItem(1,   'Add minutes/hours',            '',                 '',                 'ff0000', 'http://help.com',        0, 'Ilan',     0, 1, 0, 1));
        g.AddTaskItem(new JSGantt.TaskItem(11,  'Add support for half days',    '5/14/2009 14:00',  '5/14/2009 15:30',  'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan',  100, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(12,  'Add minute view',              '5/14/2009 16:00',  '5/14/2009 17:00',  '00ff00', '',                       0, 'Ilan',   40, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(13,  'Add hours view',               '5/14/2009 16:00',  '5/14/2009 17:00',  '00ffff', 'http://www.yahoo.com',   0, 'Ilan', 60, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(14,  'Add support for format options',               '5/14/2009 18:00',  '5/14/2009 19:00',  '00ffff', 'http://www.yahoo.com',   0, 'Shlomy', 60, 0, 14, 1));


        g.Draw();   
        g.DrawDependencies();

      }

      else

      {

        alert("not defined");

      }
</script>

UPDATE 1: I should say that Variant 1 was working when I used dhtmlxGantt library to create a chart inside the function createChartControl.

UPDATE 2: I updated my sample code in Variant 1. Still it's not working - ie alert "Start" is displayed, while alert "Fin" is not displayed.

are you sure, that response you get from updateList is JSON object?

echo json_encode(array('a'=>'b'));
die();

If you dont response JSON object, use $.get() method.

PS: please, dont use 'updateList.php?query='+'DROP DATABASE()' try some of MVC logic.

jsgantt needs global variable g to works. change var g into window.g .
so it will look like this

Variant 1:

<script>
    function createChartControl(htmlDiv)
    {
        alert("Start");

        window.g = new JSGantt.GanttChart('g',document.getElementById(htmlDiv), 'hour');

        g.setShowRes(1); // Show/Hide Responsible (0/1)
        g.setShowDur(1); // Show/Hide Duration (0/1)
        g.setShowComp(1); // Show/Hide % Complete(0/1)
        g.setCaptionType('Resource');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)
        g.setFormatArr("hours","minutes")

        if(g) {
            // Parameters (pID, pName, pStart, pEnd, pColor, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen)
            // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

            g.AddTaskItem(new JSGantt.TaskItem(1, 'Add minutes/hours', '', '', 'ff0000', 'http://help.com', 0, 'Ilan', 0, 1, 0, 1));
            g.AddTaskItem(new JSGantt.TaskItem(11, 'Add support for half days', '5/14/2009 14:00', '5/14/2009 15:30', 'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan', 100, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(12, 'Add minute view', '5/14/2009 16:00', '5/14/2009 17:00', '00ff00', '', 0, 'Ilan',  40, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(13, 'Add hours view', '5/14/2009 16:00', '5/14/2009 17:00', '00ffff', 'http://www.yahoo.com',  0, 'Ilan', 60, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(14, 'Add support for format options', '5/14/2009 18:00', '5/14/2009 19:00', '00ffff', 'http://www.yahoo.com',  0, 'Shlomy', 60, 0, 14, 1));

            g.Draw();
            g.DrawDependencies();

            alert("Fin");
        }
        else {
            alert("not defined");
        }
    }
</script>

<script>
    $(document).ready(function() {
        createChartControl('schedule');
    });
</script>
<div style="position:relative" class="gantt" id="schedule"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM