0

Is there a PHP require_once or include_once for <script>? I know <script> is pure HTML, but does PHP or HTML have such a thing?

I would like to prevent javascript from loading twice in the same page.

Example:

    <script type="text/javascript" src="js/jquery-0.4.ajaxify.min.js"></script> 
4
  • 1
    This Would Help you. stackoverflow.com/questions/2418473/… Commented Oct 10, 2016 at 16:36
  • I asked this specifically about <script>, because I want this specific behavior but for javascripts. It is so useful for php in case you load something twice, it will only load it once. Commented Oct 10, 2016 at 16:39
  • 2
    It is a javascript/html question, not a PHP question. Commented Oct 10, 2016 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. Commented Oct 10, 2016 at 17:21

2 Answers 2

1

Seems like you are looking for this:

http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload

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

2 Comments

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. :)
"Note: LazyLoad is no longer being maintained." Source
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

1 Comment

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 <script>. Does javascript have this?

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.