10

How can I just call a js function from within an html file, with no event trigger? I want to have code like:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="jquery.flot.js"></script>
<script src="chart.js"></script>

<title>
</title>
</head>

<body>

<div id="chart1" style="width:600px;height:300px"></div>

show_graph({{ chart_type }}, {{ data }}, {{ options }});

</body>

</html>

but this just results in the function call being printed to the screen, instead of the function actually being executed.

e.g. I'm getting show_graph(bar, [[1, 2000], [2, 50], [3, 400], [4, 200], [5, 5000]], ['Foo']);

What do I do?

EDIT:

I appreciate the feedback, but I tried wrapping it in a script tag and got an "invalid number of parameters" error.

the javascript is:

function show_graph(charttype, data, options){

    var chart_options = {
        series: {
            charttype: {
                show: true
            }
        }
    }

    var plot = $.plot($("#chart1"), [data], [chart_options]);
}

so I suppose the real question is "why am I getting an "invalid number of parameters" error when I'm passing 3 parameters and accepting 3 parameters?"

Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
Colleen
  • 23,899
  • 12
  • 45
  • 75

6 Answers6

15

Wrap it in <script> tags:

<body>

<div id="chart1" style="width:600px;height:300px"></div>

<script type="text/javascript">
    show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>

</body>

...though I don't know how the template factors in. I imagine it will render the same.

user113716
  • 318,772
  • 63
  • 451
  • 440
6

Yet another answer:

<script type="text/javascript">
    (function() {
        // The following code will be enclosed within an anonymous function
        var foo = "Goodbye World!";
        document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
    })(); // We call our anonymous function immediately
</script>
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
  • to all respondents: I appreciate the feedback, but I tried wrapping it in a script tag and got an "invalid number of parameters" error. the javascript is: function show_graph(charttype, data, options){ var chart_options = { series: { charttype: { show: true } } } var plot = $.plot($("#chart1"), [data], [chart_options]); } so I suppose the real question is "why am I getting an "invalid number of parameters" error when I'm passing 3 parameters and accepting 3 parameters?" – Colleen Aug 24 '11 at 02:30
5

You need to wrap the function call in script tags like this:

<script type="text/javascript">
show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>

If you need to validate as XHTML you should also wrap with CDATA. See When is a CDATA section necessary within a script tag? for a reference to that.

Community
  • 1
  • 1
Joe Landsman
  • 2,177
  • 13
  • 9
4

Put it in a <script> tag.

<script type='text/javascript'>show_graph({{ chart_type }}, {{ data }}, {{ options }});</script>

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
4

Add script tags.

<script>
show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>
j3ffz
  • 715
  • 1
  • 6
  • 17
1

You should call the function when the page is loaded. To do so with jQuery use this code:

<script type="text/javascript">
  $(document).ready(function() {
    show_graph({{ chart_type }}, {{ data }}, {{ options }});
  });
</script>
Dan Mazzini
  • 1,005
  • 10
  • 19