1

I want to add the following 2 lines only to the one page of the website

<script src="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>

And

<link href="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" media="screen" rel="stylesheet" type="text/css">

My <head> tag with all links is in a separate PHP file, which I include in all files. Is it possible to add this script and stylesheet only to one page where it's needed? On that separate PHP file, I have a function

$('.owl-carousel').owlCarousel({
   items: 1,
   nav: true,
   dots: false,
   loop: true
});
Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
Barny
  • 13
  • 3
  • 1
    How about getting current url and add only on required page. [link](https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript) – armin Dec 18 '20 at 05:23
  • Do you use any content manager system like wordpress? Maybe there is a plugin/module for do that without any workarounds. – dasiekjs Dec 18 '20 at 05:25
  • @dasiekjs No, I don't use content manager system, only php and js – Barny Dec 18 '20 at 05:30

1 Answers1

0

Just get your page url and if it is include your script and styles for that page

<?php
    $link = $_SERVER['PHP_SELF'];
    $link_array = explode('/',$link);
    $page = end($link_array);
    if($page == 'your_page_last_part') {
?>
<script src="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" media="screen" rel="stylesheet" type="text/css">
<?php } ?>
armin
  • 176
  • 5
  • 16