- 07 July 2021
- JavaScript (2)
- PHP (3)
- jQuery (2)
PHP is server side, so it’s pretty much impossible to do this without using a client-side language such as JavaScript or Ajax. In this example, we’ll be using jQuery’s load() method, so you’ll need to include the jQuery library. Example (within your head tag is usually best):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
After that, create a placeholder on your page for where you want your external content to be loaded into:
<div id="loading-area"> <p style="text-align:center">Loading...</p> </div>
Lastly, using jQuery’s load function, we’ll add this page into the placeholder created above. This will replace anything within our loading-area div, replacing it with our external page’s content/code. Put this bit at the bottom of your page so it runs after the rest of your page has loaded. Be sure to get the path to your file right:
<script>$('#loading-area').load('/testpage.php');</script>
Alternatively, wrap a window load event around it like this (jQuery below v3):
<script> $(window).load(function(){ $('#loading-area').load('/testpage.php'); }) </script>
Or like this (jQuery V3+)…
<script> $(window).on('load', function () { $('#loading-area').load('/testpage.php'); }) </script>
Comments (2)
Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.
Problem right off the bat: HOW do you load the JQuery library?