简体   繁体   中英

How to print variables which are on the header on javascript?

I have the following code that is not working correctly and I need to check what it is happening. The problem is that most of the code is done on the html header. How can I print the variables in order to check what's wrong?

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highstock Example</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
$(function() {
$.getJSON('http://localhost/teste04.php', function(data) {

    // split the data set into ohlc and volume
    var ohlc = [],
        volume = [],
        dataLength = data.length;

    for (i = 0; i < dataLength; i++) {
        ohlc.push([
            data[i][0], // the date
            data[i][1], // open
            data[i][2], // high
            data[i][3], // low
            data[i][4] // close
        ]);

        volume.push([
            data[i][0], // the date
            data[i][5] // the volume
        ]);
    }

    // set the allowed units for data grouping
    var groupingUnits = [[
        'week',                         // unit name
        [1]                             // allowed multiples
    ], [
        'month',
        [1, 2, 3, 4, 6]
    ]];

    // create the chart
    chart = new Highcharts.StockChart({
        chart: {
            renderTo: 'container',
            alignTicks: false
        },

        rangeSelector: {
            selected: 1
        },

        title: {
            text: 'AAPL Historical'
        },

        yAxis: [{
            title: {
                text: 'OHLC'
            },
            height: 200,
            lineWidth: 2
        }, {
            title: {
                text: 'Volume'
            },
            top: 300,
            height: 100,
            offset: 0,
            lineWidth: 2
        }],

        series: [{
            type: 'candlestick',
            name: 'AAPL',
            data: ohlc,
            dataGrouping: {
                units: groupingUnits
            }
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
            dataGrouping: {
                units: groupingUnits
            }
        }]
    });
});
});
    </script>
</head>
<body>
<script src="js/highstock.js"></script>
<script src="js/modules/exporting.js"></script>

<div id="container" style="height: 500px; min-width: 500px"></div>

</body>
</html>

Edit: I would like to print "datalength" for example.

Thanks in advance, Luis

variables inside the anonymous function you pass as parameter callback to $.getJSON only live inside it's scope.
You can use something like console.log(dataLength) inside that function and its value at that time will show up in the browser's console.
You can use a javascript debugger (firebug in firefox, Javascript Console in Chrome), set up a break point on the first line inside that function and execute it step by step to see the values in each variable.

Chrome, imho, has the best console and debugger, so just try this:

$.getJSON('http://localhost/teste04.php', function(data) {
    console.log(data);
    // split the data set into ohlc and volume
    var ohlc = [],
    ............

hit F12 on chrome, refresh the page and whatch all the values inside the data object in the console

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