Nader Meky
Nader Meky

Reputation: 9

how i make File.js (java script file) execute and loaded in a specific page and not loaded in other pages?

How to make java script like that loaded only in homepage and no loaded in other categories , post pages as they'have no slider .. so i no need it !

<script src='http://slideshow.googlecode.com/files/jquery-ui.min.js' type='text/javascript'/>

Upvotes: 0

Views: 117

Answers (3)

Tango Bravo
Tango Bravo

Reputation: 3309

If you don't want the script to load on a page, then don't include it. If you're using a CMS for your site, then post the CMS that you're using so we can help you.

If all that fails, you can set up something like an ID on your body tag. Then you can test the ID of the body, if it passes, execute the code, if it fails, then don't execute it.

--EDIT--

An example would be something like this..

<body id="home">
   <script language="JavaScript">
       function init() {
           if(document.getElementsByTagName('body')[0].id == 'home') {
               // We're grabbing the head and the script
               var headElement = document.getElementsByTagName('head')[0],
                   scriptElement = document.createElement('script');

               // We're setting the new script element to use the URL of the script we want
               scriptElement.type= 'text/javascript';
               scriptElement.src= 'http://slideshow.googlecode.com/files/jquery-ui.min.js';

               // We're adding this newly created element to the head
               headElement.appendChild(scriptElement);
           }
       }
       init();
   </script>
...
</body>

Try something like that. For a greater explanation for using dynamic JavaScript elements, check out this page: http://unixpapa.com/js/dyna.html

Upvotes: 1

dfsq
dfsq

Reputation: 193301

If you want to controll this behaviour on the client side I would try this approach:

<script>location.pathname == '/' && document.write('<script src="http://slideshow.googlecode.com/files/jquery-ui.min.js">\x3C/script>')</script>

But of course better to let you CMS/server code handle it for you. But based on the information you provided I can only give you client-side solution.

Upvotes: 0

Anton
Anton

Reputation: 6061

I believe you do something wrong, if you don't need script, just don't load it as Tim answered.

Anyway possible workaround, you can create script yousrcipt.js, include it in every page. In this script you can write something similar to this

if (window.location == 'whateverhomepage')  {
   var head= document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.src= 'http://slideshow.googlecode.com/files/jquery-ui.min.js';
   head.appendChild(script);
}

Upvotes: 0

Related Questions