javascript - Multiple jQuery plugins on multiple pages

797

My question may be very simple to most people on here. But i am very new to jQuery, and i am having a sort of difficulty with repetitiveness.

I have a custom script on a page which has a lot of text, that allows that text to be collapsed. This script is in jQuery. The page is in php. I have multiple pages like this. I find myself having to place the code for the script documents on every page like this:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script type="text/javascript" src="javascripts/readmore.min.js"></script>
 <script src="javascripts/main_func.js"></script>

Is there a way that i can place it only on the index.php file and call the script function which is:

<script type="text/javascript">
    $(document).ready(function(){
        $('.article').readmore({maxHeight: 640});
    });
</script>
841

Answer

Solution:

I am not sure how your page loads but this is what you would have to do essentially:

index.php

<?php
require('header.php');
require('content.php');
require('footer.php');
?>

header.php

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" >
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<meta name="viewport" content="width=device-width">
<title>Page Title</title>
<link rel="stylesheet" href="/css/default.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="javascripts/readmore.min.js"></script>
<script src="javascripts/main_func.js"></script>
</head>
<body>

content.php

<div id="content">
<!-- Do display code here -->
</div>

footer.php

<div id="footer">Footer Content</div>
</body>
</html>

If you have a new page, it would look something like this:

newpage.php

<?php
// Keep same header
require('header.php');
// New content page
require('newcontent.php');
// Same footer
require('footer.php');
?>

People are also looking for solutions to the problem: PHP - check if range appears in variable?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.