How to call a function from an .js file in the index.php itself in the script

0

I would like to know how, I call a function from an x.js file, on a page x.html x.php either.

Example:

x.js file:

function myFunction(img){
    // Faz algo
}

Index.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <script>

        myFunction("algo.jpg"); // <-- como fazer uma função de um arquivo js, aceitar eu chamar ela em um arquivo index.php?

    </script>
</head>
<body>
     <img src="img/algo.jpg" alt="">

     <script src="x.js"></script>
</body>
</html>
    
asked by anonymous 25.10.2017 / 00:21

2 answers

1

This is very common here, and the question is this, you are trying to access a method that is not yet in the document, because you are importing the Javascript file at the end of the document. p>

Do the following put the line

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

Before:

<script>

    myFunction("algo.jpg"); // <-- como fazer uma função de um arquivo js, aceitar eu chamar ela em um arquivo index.php?

</script>
  

See working at plnkr.co

    
25.10.2017 / 01:04
0

You can only do this using script[src] :

<script src="PATH_TO.js" type="text/javascript"></script>
    
25.10.2017 / 00:31