Hello Danny the Hopeless,
My first tasks were related to the revision of the onload event in the div#loadThisWhenPageLoads element and as DanieleAlessandra pointed out this is not an effective approach.
Next, in reviewing your question thoroughly, I realized that your actual goal here seems to be merely able to achieve the functionality that shows and removes a Bootstrap carousel depending on the screen size. Now that is a different task altogether in my view. With that said, I think an approach that leverages the CSS3 display property and @media queries is the most practical and effective way to resolve the task at hand. Below you can find the code I created for this particular purpose.
JSFiddle Resolution Demo:
JSFiddle link demo
Full Code Snippet:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!--
Bootstrap CSS Loaded from CDN.
Note: You can load the Bootstrap CSS from anywhere you want (CDN, locally, etc.). In my case, I am using a CDN as it is easy to implement so I can quickly move on and work on what matters -resolving the issue at hand.
-->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Arty's StackOverflow Question 56740953</title>
</head>
<body>
<h1>StackOverflow Question 56740953 Resolution</h1>
<h2>Goal</h2>
<p>
Load a Bootstrap carousel only if the screen size is larger than 768px (larger than a mobile device).
</p>
<h3>Basic Approach (CSS3 display/media-query):</h3>
<ol>
<li>Load hidden carousel (display: none). </li>
<li>Use CSS3 media query to show carousel when screen size is larger than desired with (768px).</li>
</ol>
<h2>Resize browser screen to see demo in action:</h2>
<style>
/*
** Micro-style choice to improve body visibility.
** Remove this style script when ready to use and resize body as needed.
*/
body {
padding: 20px;
}
/*
** Micro-style choice to reduce slider size for this demo.
** Remove this style script when ready to use and resize carousel as needed.
*/
#carouselExampleIndicators {
width: 80vh;
margin: 0 auto;
}
</style>
<!--
Bootstrap Carousel Example With indicators.
Source: https://getbootstrap.com/docs/4.0/components/carousel/
Note: I am using "placeimg" to load dummy images easily for this demo.
Source: https://placeimg.com/
-->
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="https://placeimg.com/640/480/any" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/640/480/any" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/640/480/any" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<style>
#carouselExampleIndicators {
display: none;
}
@media screen and (min-width: 768px) {
#carouselExampleIndicators {
display: block; /* "block" is the default display property value for this Bootstrap carousel div. */
}
}
/*
The following sources are recommended concerning the CSS3 display property and @media queries. Also,please note that the order matters when it comes to the @media queries so moving the @media... piece of code arbitrarily anywhere before the code that defines the styles for the elements referred within the @media styles can affect the functionality detrimentally.
**
** Source: https://www.w3schools.com/css/css_display_visibility.asp
** Source: https://www.w3schools.com/css/css3_mediaqueries.asp
*/
</style>
<!--
Bootstrap JavaScript Bundle Loaded from CDN.
Note: You can load the Bootstrap CSS from anywhere you want (CDN, locally, etc.). In my case, I am using a CDN as it is easy to implement so I can quickly move on and work on what matters -resolving the issue at hand.
Source: https://www.bootstrapcdn.com/
-->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
</body>
</html>
This as certainly an interesting challenge. If you have any issues implementing the approach I have recommended please feel free to let me know and I would be glad to assist you further. Also, if for whatever reason you are interesting in continuing to seek an approach that is related loading the carousel element via Javascript and/or inserting PHP code dynamically to accomplish this end, let me know as well and I would look into this matter further with you. There are certainly many paths to web development and the CSS3 approach seemed the most straightforward for the challenge you have at hand, which is why I have recommended it, but I am happy to help you look into other alternate solutions that might better suit your specific needs.
Regards,
Arty