1

I've a JS file with function named graph_poids2, this function has an argument named dayr:

File graph.js:

$(function graph_poids2(dayr) {
  alert(dayr);
  chaine1='../graph/data_today_poids2.php?day=';
  alert(chaine1.concat(dayr));
  $.getJSON(chaine1.concat(dayr), function (data) {  // on récupère les data du json.

  etc..
}

In my Main PHP file and before exisiting an argument in JS function graph_poids2, I called the JS fuction like that:

<script src="../graph/graph.js"></script>

BUT now how to do with the JS argument? I've modified the PHP file like that, but it's not working :

<script type="text/javascript"> var dayr='<?php echo "2017-10-31"; ?>'; </script>
<script type="text/javascript"> graph_poids2(dayr); </script>
<script src="../graph/graph_today_poids2.js"></script>

How to do , i've tried many things and looked on the web but nothing help me... Any ideas , please?

1
  • 1
    Press F12 in your browser, you can see a develop console. Then refresh the tab, you can get specific error logs. Post the errors in your question we would help you. Commented Nov 2, 2017 at 10:36

1 Answer 1

1

Reverse the appending of your scripts to the page. Now graph_poids2(dayr); gets called before the script with the function has been added to the DOM.

<script src="../graph/graph_today_poids2.js"></script>
<script type="text/javascript"> 
    var dayr='<?php echo "2017-10-31"; ?>'; 
    graph_poids2(dayr);
</script>

Script blocks are parsed in line with the page flow. JavaScript content within a script block is parsed according to the rules set for the language. Functions and vars get hoisted to the top. So declaring a function below the line that calls the function isn't a problem within a script block, but not when the function is called from another script block.

also it is good practice to use an onload event to make sure all necessary scripts have been loaded. Jquery makes this easy with $(document).ready(«function») but in Vanilla JS you can do this to with the load event.

Sign up to request clarification or add additional context in comments.

3 Comments

F12 Console, said the function graph_poids2 isn't defined, and alert popup doesn"t display a correct value for dayr argument
Well check if there is a 404 error on your resource or maybe even a script error in graph_today_poids2.js.
If I fix dayr arguemnt in JS function like : dayr='2017-11-01';, the script is well executed (with inside function fixed arguement), but remaining the the warning graph_poid2(dayr) not defined.

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.