简体   繁体   中英

How to call custom.js file in for loop and i want pass some argument with this js

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'">
}

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?

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