0

How to call customs.js file in for loop and i want pass some argument with this js,And how to retrieve this var in JavaScript file.

      for($i=0;$i<5;$i++)
{


  <script type="text/JavaScript" src="http://localhost/customs.js?var=test'">
}

1 Answers1

1

A better way to do this would be to include your custom.js in the html of the webpage and wrap it's code in a function to which you can pass a variable, like so:

<html>
    <head>
        <script type="text/JavaScript" src="http://localhost/customs.js"></script>
    </head>
    <body>
        <!-- Your HTML -->
        <script>
            <?php 
                 for($i=0; $i<5; $i++){
                      echo "custom(".$myvar.");";
                 }
            ?>
        </script>
    </body>
</html>

Then in your custom.js file you can do this:

function custom(input){
    //Do something
}

I should note that you should be careful inserting unsanitized PHP into html directly. For more info, see: How to prevent XSS with HTML/PHP?

Community
  • 1
  • 1
winhowes
  • 7,845
  • 5
  • 28
  • 39