javascript filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mgershma@hotmail.com

    javascript filename

    Hi.

    Does anybody know how can JavaScript get the filename of the file
    it's
    located in?

    For example, file 'mike.js' contains several functions and
    variables. I would like to set the variable to the filename of this
    file dynamically:

    var sourceFileName = "mike.js";




    Thank you in advance,


    Mike




    *************** ************

    MichaelGershman .com

    Life Is Life...

    *************** ************

  • Stephen Chalmers

    #2
    Re: javascript filename


    <mgershma@hotma il.com> wrote in message news:1123611749 .356860.138810@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > Hi.
    >
    > Does anybody know how can JavaScript get the filename of the file
    > it's
    > located in?
    >
    > For example, file 'mike.js' contains several functions and
    > variables. I would like to set the variable to the filename of this
    > file dynamically:
    >
    > var sourceFileName = "mike.js";
    >[/color]

    Once loaded from a .js file, a script becomes a part of the document that loaded it, just as if the script had been
    coded directly into the document at the position of the <script src=...> tag.
    After loading the script from an external file, no further reference is made to that file or its contents.
    Yes - really.

    --
    Stephen Chalmers


    Comment

    • RobG

      #3
      Re: javascript filename

      mgershma@hotmai l.com wrote:[color=blue]
      > Hi.
      >
      > Does anybody know how can JavaScript get the filename of the file
      > it's
      > located in?[/color]

      You could try removing script elements with src attributes one at a time
      then call the function. If the call fails you could guess that the
      failure was because you removed that particular element. But it would
      not be very pretty or robust I expect.
      [color=blue]
      >
      > For example, file 'mike.js' contains several functions and
      > variables. I would like to set the variable to the filename of this
      > file dynamically:
      >
      > var sourceFileName = "mike.js";
      >[/color]

      You can create new script elements and give them a src attribute that
      contains the URI of a script file:

      var oScript = document.create Element('script ');
      oScript.type = 'text/javascript';
      oScript.src = 'mike.js';
      document.body.a ppendChild( oScript );


      --
      Rob

      Comment

      • Csaba Gabor

        #4
        Re: javascript filename

        mgershma@hotmai l.com wrote:[color=blue]
        > Does anybody know how can JavaScript get the filename of the file
        > it's located in?[/color]

        I didn't try this, but...
        script elements live under the documentElement element, regardless of
        where they're defined in the document. So you might think to fish them
        all out with
        getElementsByTa gName. Furthermore, script elements are created at the
        time they are encountered.

        Therefore, you could place code at the top of your .js file to iterate
        through all the current script elements and take the last one
        encountered. Then check the .src on that element:

        var aScripts;
        aScripts=docume nt.documentElem ent.getElements ByTagName('scri pt');
        var lastIdx;
        for (var idx in aScripts) lastIdx=idx;
        alert ("I'm in " + aScripts[idx].src);

        Hope it helps,
        Csaba Gabor from Vienna

        Comment

        • huchengtw@gmail.com

          #5
          Re: javascript filename

          Dear,

          Using oScript.src may be a great solution to solve it. But, in my
          experience, HTML Document in IE will download it asynchronously. So, if
          you set oScript.src and evaluate the variables or functions
          immediately, you will get an undefined exception.

          Because my js just runs in IE. So, I use an ActiveX Object (XMLHTTP
          object) to download the source code. After I get the source code, I
          append it to oScript.text. In this way, IE will load these source code
          immediately, and you can use them right now.

          ex:

          var objHTTP = new ActiveXObject(" Msxml2.XMLHTTP. 4.0");
          objHTTP.open("G ET", filename, false);
          objHTTP.send();
          if(objHTTP.read yState == 4)
          {
          oScript.text=oS cript.text + objHTTP.respons eText;
          }

          var oObj=eval(funct ion name);


          RobG 寫道:
          [color=blue]
          > mgershma@hotmai l.com wrote:[color=green]
          > > Hi.
          > >
          > > Does anybody know how can JavaScript get the filename of the file
          > > it's
          > > located in?[/color]
          >
          > You could try removing script elements with src attributes one at a time
          > then call the function. If the call fails you could guess that the
          > failure was because you removed that particular element. But it would
          > not be very pretty or robust I expect.
          >[color=green]
          > >
          > > For example, file 'mike.js' contains several functions and
          > > variables. I would like to set the variable to the filename of this
          > > file dynamically:
          > >
          > > var sourceFileName = "mike.js";
          > >[/color]
          >
          > You can create new script elements and give them a src attribute that
          > contains the URI of a script file:
          >
          > var oScript = document.create Element('script ');
          > oScript.type = 'text/javascript';
          > oScript.src = 'mike.js';
          > document.body.a ppendChild( oScript );
          >
          >
          > --
          > Rob[/color]

          Comment

          • Dr John Stockton

            #6
            Re: javascript filename

            JRS: In article <1123611749.356 860.138810@f14g 2000cwb.googleg roups.com>
            , dated Tue, 9 Aug 2005 11:22:29, seen in news:comp.lang. javascript,
            mgershma@hotmai l.com <mgershma@hotma il.com> posted :[color=blue]
            >
            > Does anybody know how can JavaScript get the filename of the file
            >it's
            >located in?
            >
            > For example, file 'mike.js' contains several functions and
            >variables. I would like to set the variable to the filename of this
            >file dynamically:
            >
            > var sourceFileName = "mike.js";[/color]

            Include that statement in that file, preferably at the beginning.

            If you vary file-names in an arbitrary fashion, write a simple tool on
            your editing system that updates such lines at need.

            If one Web page calls more than one *.js file, the variable will hold
            the name of the most recently invoked file.


            Alternatively : the name of the *.js file is given by being included in
            an HTML "statement" such as
            <script type="text/javascript" src="include1.j s"></script> .

            Rather than writing that all literally in HTML, write it with javascript
            using a variable for the file name.

            <script type="text/javascript">
            St = "include1.j s"
            document.writel n(
            '<script type="text/javascript" src="', St, '"><\/script>')
            </script>

            Test :
            <script type="text/javascript">
            document.write( LZ.toString())
            </script>

            That, for me, writes the code of LZ (which is in include1.js), showing
            that a file whose name is given by St has been included.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...