SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    conditonal import of external js

    I want to use if and else statements to conditonally import an external js script but I am not sure on the syntax.

    <script type='text/javascript'>
    if (location.host == 'myurl'){
    *** import script***
    }else{
    document.write(location.host);
    }
    </script>


    should I use: <script src="script.js">
    making it:

    <script type='text/javascript'>
    if (location.host == 'myurl'){
    <script src="script.js">
    }else{
    document.write(location.host);
    }
    </script>

    or what other function should I use to execute external script?

  2. #2
    SitePoint Member SkinnyK's Avatar
    Join Date
    Sep 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not 100&#37; sure but maybe give this a go? I am not sure what you mean by location.local..

    Code JavaScript:
    <script type='text/javascript'>
    if (variable == condition) {
      document.write('<script type="text/javascript" src="javascript.js"></script>');
    } else {
      //do whatever
    }
    </script>

    However, I think this will only work if the script is located in the header. If you want it somewhere else after the page is loaded, you need to have the JS file already linked, then use a script within the document that calls a function depending on a condition, instead of a script. Hope I helped
    If you need someone to talk to, I'm always there to listen.


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
  •