SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    test for the existence of prototype.js?

    i am using a hosted CMS solution that uses prototype for some of its components. it only includes the prototype.js library in certain cases which are dependent on the content that a user adds to their page.

    this is a problem because i want to write my own scripts for the site which make use of prototype, and if prototype is only sometimes included then my scrips wont always work.

    so i am wondering if there is a way to write a javascript that will check to see if prototype has been included, and if it hasn't then include it.

    does anybody know ho i could do this?

  2. #2
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Canada
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do the trick:

    Code JavaScript:
    if(typeof Prototype=='undefined') {
      document.write('<script type="text/javascript" src="/path_to/prototype.js"></script>');
    }

    You have to use 'brute force' here because Safari doesn't like DOM insertion of scripts.

    Beware that some browsers may not use their cached copy of prototype if you do it this way. If there is any way to persuade the CMS provider to include it by default on all pages, which won't cause a performance hit if they set the cache headers correctly, that will be much smoother for your users.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tigerincanada View Post
    This should do the trick:
    Beware that some browsers may not use their cached copy of prototype if you do it this way. If there is any way to persuade the CMS provider to include it by default on all pages, which won't cause a performance hit if they set the cache headers correctly, that will be much smoother for your users.
    i have spoken with them and they are actually working on a fix, but in the meantime i have a client who is waiting for me to finish builtign their site.

    i will give you method a try - thanks for the reply!

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i keep getting an "unterminated string literal" error on the following line:

    Code:
    document.write('<script type="text/javascript" src="/Websites/ndbg/templates/NDBG/scripts/prototype.js"></script>');
    not sure why. any thoughts?

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use that code within a <script> element (rather than in an external JavaScript file), you must escape the '</' character sequence:
    Code JavaScript:
    if(typeof Prototype=='undefined') {
      document.write('<script type="text/javascript" src="/path_to/prototype.js"><\/script>');
    }
    (Note that the end tag is written as <\/script>.)
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply - that seemed to do the trick.

    now that i have that working i am wondering how i would do something similar to check for the existence of scriptaculous elements - specifically effects.js.

    someone on another forum suggested that i could just include effects.js even if it has already been included and that if it was included twice it would not cause a conflict. but i haven't tried that yet - can anyone confirm that?

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the same way as was suggested for Prototype, test for the existence (or lack of undefined-ness) of one or more of the functions/objects/classes created in the file, so something like:
    Code:
    if(!Effect) {
    
    }
    will work. Including it again shouldn't cause any problems, it doesn't actually perform any actions but just provided methods you can use.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •