Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Conditional Loading of External JS File

    Hello,

    I'm looking for a way to load and execute an external javascript file only if a certain condition is true, for example, I'm looking for the proper way to execute this pseudo-code:

    if a == true {
    <script language="javascript" src="http://www.xyz.com/myscript.js"></script>
    } else {
    document.write("too bad")
    }

    Can someone let me know how this is done?

    thanks,

    JB
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Depends - when do you want to write it, while the page is loading or when it's finished loading?
    Code:
    // While loading:
    if(a==true){
        document.write('\u003cscript language=\u0022javascript\u0022 \
    src=\u0022http://www.xyz.com/myscript.js\u0022>\u003c/script>');
    }else{
        document.write("too bad")
    }
    Or:
    Code:
    // When finished loading:
    function fnLoadScript(url){
        var
            t='text/javascript',
            q='\u0022',
            d=document,
            n=navigator,
            e;
        if(/mac/i.test(n.platform)&&/msie/i.test(n.userAgent))// if ie5m, do it this way
            (d.createElement('div')).innerHTML='\u003cscript type='+q+t+q+' src='+q+url+q+'>\u003c/script>';
        else{
            (e=d.createElement('script')).src=url;
            e.type=t;
            d.getElementsByTagName('head')[0].appendChild(e)
        }
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Excellent, thanks.

    I will be loading it while the page is loading, so I use the first code.

    I do have one question, though - wait! I just answered, u0022 is probably a quotation mark or something.

    Thanks again.

    JB
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Originally posted by johnnyb
    I just answered, u0022 is probably a quotation mark or something.
    Congratulations - you just won a shoebox to carry your shoes home in next time you buy shoes - can be cashed out in every shoe shop in town!

    (When you put scripts in HTML, you must replace <, ", &amp; with &amp;lt;, &amp;quot;, &amp;amp; or escape them in the script. Not all browsers expand entities in scripts, while most handle unicode escapes in javascript.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    be there a way to remove the script?
    i'm programming something like a game console. the user loads a game in the js file.
    when the user switches games, can i remove the old one?

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have to do it dynamically I think. Something like:
    Code:
    sc = document.createElement("script");
    sc.src = "something.js";
    document.body.appendChild(sc);
    You have to remove it later by:
    Code:
    document.body.removeChild(sc);
    If you document.write, I don't think you can remove it later, unless you refresh the page.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You can remove the script element from the DOM tree, you can not, however, undo anything the script did, including declaring functions and variables that didn't get used.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i can remove the script tag, but the functions will still be accessible?

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yep. That's the way it works. If you want to delete the functions, use delete functionName; or functionName=null;.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    another application of this...

    So, I'm making some web pages that are viewed locally, (off a CD), but interact with a web server to fill a person's shopping cart.

    I am planning on using the method of loading a javascript file after the page has been loaded that lilorean gives near the beginning of this thread, and having the javascript file that is called be an ASP file that adds stuff to the shopping cart based on the querystring that is passed to it, (constructed by javascript on the original page).

    While I know that this will work, I am wondering if there is a better way of using local javascript to send commands to a web server. This could get rid of some caching issues in IE.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Last edited by Vladdy; 05-23-2003 at 03:08 AM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #12
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Liorean,

    How's browser-support for Your loading mechanisme?

    Jerome

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The first? Should have browser support to nn4 and later. As long as the browser supports unicode JavaScript escape sequences it works. You can of course change the escape sequence to be an ASCII hex code instead, but where lies the fun in that?

    The second import mechanism, however, only works in moz, iem and iew, unless saf/konq has begun supporting it too. Op7 doesn't support it for two reasons that I won't go into detail on unless you ask for them. That's pretty much all the DOM enabled browsers except for iCab, about which I have no idea.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Liorean!

    For option 2, load after the document has finished loading I have 2 questions:

    Q1.
    In the head of my document I already have a script external-src declaration, no problem with Your loading mechanisme it will append another one, and works fine. The question is how to remove only the one Your mechanisme appends and not the other one or both. Can I give them an ID maybe?

    Q2.
    Do I need to check if the external file is fully loaded (in the past I used a function for it) to read-out an array or is there like xml-file retrieving a special declaration for it (xml_Doc.readyState==4 which to my idea works well)

    Thanks in advance,
    Jerome

  • #15
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    A1: Not really sure how you mean - it appends a new script element to the head, and you can remove that script element form the head as you wish using lastChild. However, as I mentioned above, you will only have removed the script tag, you haven't undone anything the script does or defines for later use. This is especially evident in the iem fork, that never even appends the script element to the document.

    A2: One of the problems with it is that there is no way for you to know that the file is loaded. That's why you should have all direct calls to it in the file itself or in functions that are called by events/timed calls.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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