0

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'/>

2
  • you have to be more specific in the way the home page is distinguished from others. Are you using server side PHP, .Net...? Commented Jan 31, 2012 at 12:14
  • Yeah, this is not a JavaScript problem. This is server-side problem. If you are somewhat restricted in modyfing server-side, then there is nothing you can do. If you are not, then you have to write a PHP/.NET/Python/Perl/Ruby/whatever/batman/watman script which will take care of this. Probably you will use some fancy templating system. Commented Jan 31, 2012 at 12:17

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me how to make id to body and attatch it with this js ? i use blogger !
It's pretty simple to make an ID on a body. Just do it like this: <body id="home"> That's all you have to do. I'll edit my answer to give an example function. But I'm a bit wary to do so. I'd rather have you understand what you're doing, and work out the solution on your own, than have me write you a solution, and you not get why it works.
i attach it like that ?? <body id="home"><script src='slideshow.googlecode.com/files/jquery-ui.min.js' type='text/javascript'/></body> ??
0

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);
}

Comments

0

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.

1 Comment

XML: The entity name must immediately follow the '&' in the entity reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.