Load a script and process it inmediately

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jesús Ángel

    Load a script and process it inmediately

    Hello.

    I have a script (S1) that depends on other script (S0) so I have to
    include both scripts in all pages:

    <script type="text/javascript" src="/js/S0.js"></scrip>
    <script type="text/javascript" src="/js/S1.js"></scrip>

    I have read that is is posible to load one script from any other:

    function loadScript (scriptUrl) {
    var head = document.getEle mentById("head" );
    var script = document.create Element("script ");
    script.type = "text/javascript";
    script.src = url;
    head.appendChil d(script);
    }

    Supose that I want to load S1 from S0:
    S0.js:
    ../..
    loadScript('S1. js');
    s1Function(); // A function defined inside S0

    index.shtml:
    <head>
    <script src="S0.js">
    </head>

    The code above doesn't work because S1.js it is loaded after the end of
    S0.js and not when I load S1.js with loadScript.

    ¿It is posible to load S1.js right after invoke loadScript('S1. js') so I
    could use S1 functions' in S0.js?

    Thanks.
  • Thomas 'PointedEars' Lahn

    #2
    Re: Load a script and process it inmediately

    Jesús Ángel wrote:
    [color=blue]
    > I have a script (S1) that depends on other script (S0) so I have to
    > include both scripts in all pages:
    >
    > <script type="text/javascript" src="/js/S0.js"></scrip>
    > <script type="text/javascript" src="/js/S1.js"></scrip>[/color]

    There is a "t" missing in both end tags, but maybe that's only a typo
    duplicated by copy and paste.
    [color=blue]
    > I have read that is is posible to load one script from any other:
    >
    > function loadScript (scriptUrl) {[/color]

    if (document.getEl ementsByTagName )
    {
    [color=blue]
    > var head = document.getEle mentById("head" );[/color]

    Instead:

    var head = document.getEle mentsByTagName( "head");
    if (head && (head = head[0]) && document.create Element)
    {
    [color=blue]
    > var script = document.create Element("script ");[/color]

    if (script && head.appendChil d)
    {
    [color=blue]
    > script.type = "text/javascript";
    > script.src = url;
    > head.appendChil d(script);[/color]

    }
    }
    }[color=blue]
    > }
    >
    > Supose that I want to load S1 from S0:
    > S0.js:
    > ../..
    > loadScript('S1. js');
    > s1Function(); // A function defined inside S0
    >
    > index.shtml:
    > <head>
    > <script src="S0.js">
    > </head>
    >
    > The code above doesn't work because[/color]

    there are the following items missing:

    - the "id" attribute value "head" for the "head" element, if you
    want to use getElementById( ) instead of getElementsByTa gName()
    - the "title" element
    - the "type" attribute value for the "script" element
    - the </script> tag.
    [color=blue]
    > S1.js it is loaded after the end of
    > S0.js and not when I load S1.js with loadScript.[/color]

    No, see above.
    [color=blue]
    > ¿It is posible to load S1.js right after invoke loadScript('S1. js') so I
    > could use S1 functions' in S0.js?[/color]

    No, because that would mean that both script files would depend on each
    another (cross dependency). loadScript() only works within HTML files
    and on few browsers, i.e. only if there is a document which provides
    the object model that is required by the method.


    PointedEars
    --
    But I thought you'd like it...

    Comment

    Working...