Is there a way to stop ` – Sol Oct 10 '16 at 16:39

  • 2
    It is a javascript/html question, not a PHP question. – chris85 Oct 10 '16 at 16:42
  • You are right, I have it listed as html and javascript. I thought PHP might have a solution also for the a possible double loading issue. I changed the question to be more inclusive of javascript and html. – Sol Oct 10 '16 at 17:21
  • 2 Answers2

    1

    One of the way you can run script tags in php would be

    <?php  
    ....Here is php code
     ?>
       ... add the  script here(<script>....</script>
     <?php
      ?>
    

    Another probable way is :

    Using php inside the script tags for example:

    <script type="text/javascript">
      var alertMsg = '<?php echo $custom_message; ?>';  
       alert(alertMsg);
     </script>
    

    If you are using buttons

    echo('<button type="button" onclick="customfunction();">My Button</button>');
    
    <script>
    //call your customfunction here
    
    </script>
    

    AN UPDATE TO SOLVE LOADING SCRIPT CONTENTS TWICE

    I would suggest use of javascript function which are called to the specific page as they are needed an example would be

    Create a file with a .js eg

    example.js
    here declare your functions you would put in the script tags of a html page
    

    In the page you want to use the <script>

    Include the example.js file and you can call the custom functions from there use the obect orientend approach
    

    Check This resource for more info about obect orientend javascrip

    Mr Lister
    • 45,515
    • 15
    • 108
    • 150
    Geoff
    • 6,277
    • 23
    • 87
    • 197
    • Let's say you load the same javascript twice. This is why you would use require_once. It will only load the php once instead of twice. This is what my goal was for – Sol Oct 10 '16 at 17:20
    1

    Seems like you are looking for this:

    http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload

    Raphael
    • 770
    • 1
    • 7
    • 23
    • That's cool! Too bad there isn't a built in javascript function. We have 1000's of pages of code, so sometimes you aren't sure if something gets called twice. :) – Sol Oct 10 '16 at 19:39
    • 1
      "Note: LazyLoad is no longer being maintained." [Source](https://github.com/rgrove/lazyload/) – winterfruit Oct 11 '16 at 04:53