How to get the Javascript file name from a function inside the file.

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

    How to get the Javascript file name from a function inside the file.

    I know the subject might be confusing. I am no beginner with
    javascript but I haven't been able to figure out how to get the
    javascript file name from code inside the file. So you have an HTML
    doc with script tag who's source is a javascript file.

    <HTML>
    <script src="javascript .js"></script>
    </HTML>

    Javascript.js
    -------------
    code to get the javascript.js file name?

    Is there a way to get the filename in which the code is being executed?
    Thanks

  • Dr John Stockton

    #2
    Re: How to get the Javascript file name from a function inside the file.

    JRS: In article <1103223283.672 398.268790@z14g 2000cwz.googleg roups.com>
    , dated Thu, 16 Dec 2004 10:54:43, seen in news:comp.lang. javascript,
    ryanmhuc@yahoo. com posted :[color=blue]
    >I know the subject might be confusing. I am no beginner with
    >javascript but I haven't been able to figure out how to get the
    >javascript file name from code inside the file. So you have an HTML
    >doc with script tag who's source is a javascript file.
    >
    ><HTML>
    ><script src="javascript .js"></script>
    ></HTML>
    >
    >Javascript.j s
    >-------------
    >code to get the javascript.js file name?
    >
    >Is there a way to get the filename in which the code is being executed?[/color]

    Put var ThisIs = "javascript .js"
    inside file javascript.js - or try rephrasing the question.

    --
    © 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

    • Randy Webb

      #3
      Re: How to get the Javascript file name from a function inside thefile.

      ryanmhuc@yahoo. com wrote:
      [color=blue]
      > I know the subject might be confusing. I am no beginner with
      > javascript but I haven't been able to figure out how to get the
      > javascript file name from code inside the file. So you have an HTML
      > doc with script tag who's source is a javascript file.
      >
      > <HTML>
      > <script src="javascript .js"></script>
      > </HTML>
      >
      > Javascript.js
      > -------------
      > code to get the javascript.js file name?
      >
      > Is there a way to get the filename in which the code is being executed?[/color]

      IE and Opera support the document.script s collection and you can query
      its src property. Mozilla won't give it that easy.

      In testing locally, IE gives just the filename, Opera gives the full path.

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • RobG

        #4
        Re: How to get the Javascript file name from a function inside thefile.

        ryanmhuc@yahoo. com wrote:
        [...][color=blue]
        >
        > Is there a way to get the filename in which the code is being executed?
        > Thanks
        >[/color]

        In addition to the methods above, put this in a form:

        <input type="button" value="click" onclick="
        var m,
        foundOne = false,
        msg = '';
        if (!(m = document.script s))
        m = document.getEle mentsByTagName( 'script');
        for (var i=0; i<m.length; i++) {
        if(m[i].src) {
        msg += '\n' + m[i].src;
        foundOne = true;
        }
        }
        (foundOne)? alert(msg):aler t('No script files');
        ">


        Lightly tested in Firefox and IE, only works if either the scripts
        collection or getElementByTag Name is supported. You may want to extend
        it further for other cases.

        --
        Rob

        Comment

        • ryanmhuc@yahoo.com

          #5
          Re: How to get the Javascript file name from a function inside the file.

          Thanks for the idea but this is going to be for a templating class
          system and the document name will be changing constantly. I would like
          to try to automate the file name retrieval so it does not have to be
          manually inputted.

          Dr John Stockton wrote:[color=blue]
          > JRS: In article[/color]
          <1103223283.672 398.268790@z14g 2000cwz.googleg roups.com>[color=blue]
          > , dated Thu, 16 Dec 2004 10:54:43, seen in news:comp.lang. javascript,
          > ryanmhuc@yahoo. com posted :[color=green]
          > >I know the subject might be confusing. I am no beginner with
          > >javascript but I haven't been able to figure out how to get the
          > >javascript file name from code inside the file. So you have an HTML
          > >doc with script tag who's source is a javascript file.
          > >
          > ><HTML>
          > ><script src="javascript .js"></script>
          > ></HTML>
          > >
          > >Javascript.j s
          > >-------------
          > >code to get the javascript.js file name?
          > >
          > >Is there a way to get the filename in which the code is being[/color][/color]
          executed?[color=blue]
          >
          > Put var ThisIs = "javascript .js"
          > inside file javascript.js - or try rephrasing the question.
          >
          > --
          > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00[/color]
          IE 4 ©[color=blue]
          > <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of[/color]
          news:comp.lang. javascript[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates,[/color]
          sources.[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ[/color]
          items, links.

          Comment

          • ryanmhuc@yahoo.com

            #6
            Re: How to get the Javascript file name from a function inside the file.

            Thanks for the idea but I fail to see how the document.script s object
            can give me the name of the script that code is running in? The script
            object contains an array of sciprt objects. Each script object has a
            src property which has the script name but if there is more then 1
            script file loaded how would you identify which script object you code
            is running under - Given that your code does not know what the file
            name is from an manually inputted variable?

            Comment

            • ryanmhuc@yahoo.com

              #7
              Re: How to get the Javascript file name from a function inside the file.

              Thanks for the idea Rob but again how can you retrieve the proper
              script object from the code being executed. The idea is automate the
              retrieval of the script file name from code executing with the file.
              So if you could retrieve the file name from the executing code to
              compare it to the src property of a script object in the scripts
              collection then you wouldn't need to iterate through the scripts
              collection in the first place.

              Comment

              • McKirahan

                #8
                Re: How to get the Javascript file name from a function inside the file.

                <ryanmhuc@yahoo .com> wrote in message
                news:1103223283 .672398.268790@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                > I know the subject might be confusing. I am no beginner with
                > javascript but I haven't been able to figure out how to get the
                > javascript file name from code inside the file. So you have an HTML
                > doc with script tag who's source is a javascript file.
                >
                > <HTML>
                > <script src="javascript .js"></script>
                > </HTML>
                >
                > Javascript.js
                > -------------
                > code to get the javascript.js file name?
                >
                > Is there a way to get the filename in which the code is being executed?
                > Thanks
                >[/color]

                It's not clear to me what you want.

                "Is there a way to get the filename in which the code is being executed?"

                You want to get the name of the JavaScript "include" file when?

                Also, what if the following is the case?

                <HTML>
                <script src="javascript 1.js"></script>
                <script src="javascript 2.js"></script>
                </HTML>


                Comment

                • Ryan Hubbard

                  #9
                  Re: How to get the Javascript file name from a function inside the file.

                  Perhaps my first post was not clear so hopefully this should clear it
                  up.

                  You have a javascript file that is included via the script tag in an
                  HTML file like so.
                  <SCRIPT language='javas cript' src='somejavasc ript.js'></script>

                  At runtime any code in this javascript.js file will be executed. Is
                  there a way to programmaticall y via the code in the somejavascript. js
                  file to determine the javascript file name it came from. So the code in
                  the somejavascript. js file should determine that it is within the
                  somejavascript. js file. I know you could just code it in but where
                  attempting to automate this process hence the term programmaticall y.
                  Hope this clears up any confusion.


                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • McKirahan

                    #10
                    Re: How to get the Javascript file name from a function inside the file.

                    "Ryan Hubbard" <ryanmhuc@yahoo .com> wrote in message
                    news:41c30e64$1 _1@127.0.0.1...[color=blue]
                    > Perhaps my first post was not clear so hopefully this should clear it
                    > up.
                    >
                    > You have a javascript file that is included via the script tag in an
                    > HTML file like so.
                    > <SCRIPT language='javas cript' src='somejavasc ript.js'></script>
                    >
                    > At runtime any code in this javascript.js file will be executed. Is
                    > there a way to programmaticall y via the code in the somejavascript. js
                    > file to determine the javascript file name it came from. So the code in
                    > the somejavascript. js file should determine that it is within the
                    > somejavascript. js file. I know you could just code it in but where
                    > attempting to automate this process hence the term programmaticall y.
                    > Hope this clears up any confusion.
                    >
                    >
                    > *** Sent via Developersdex http://www.developersdex.com ***
                    > Don't just participate in USENET...get rewarded for it![/color]


                    First, the following is the current standard:

                    <script type='text/javascript' src='somejavasc ript.js'></script>

                    Second, the following is not a true statement:

                    "At runtime any code in this javascript.js file will be executed."

                    It may declare functions that may or may not be executed.

                    Third, what's wrong with JRS' suggestion of just placing a variable in the
                    file that identifies the name of the file?

                    Fourth, could you be more clear about "where [sic] attempting to automate
                    this process"?


                    Comment

                    • Ryan Hubbard

                      #11
                      Re: How to get the Javascript file name from a function inside the file.



                      I should change something from my last post. It should not be at runtime
                      but rather it should be able to determine the file name inside a
                      function in that file is when it is called. Furthmore the attempt,
                      naturally, is to make this as cross browser compatible as possible
                      (except for Netscape 4.x cause that browser should just never had been
                      created.)

                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      • Grant Wagner

                        #12
                        Re: How to get the Javascript file name from a function inside the file.

                        "Ryan Hubbard" <ryanmhuc@yahoo .com> wrote in message
                        news:41c311e5$1 _1@127.0.0.1...[color=blue]
                        >
                        >
                        > I should change something from my last post. It should not be at[/color]
                        runtime[color=blue]
                        > but rather it should be able to determine the file name inside a
                        > function in that file is when it is called. Furthmore the attempt,
                        > naturally, is to make this as cross browser compatible as possible
                        > (except for Netscape 4.x cause that browser should just never had been
                        > created.)[/color]

                        Then it's simple:

                        Inside file1.js you'd have:

                        function1() {
                        var thisFile = 'file1.js';
                        // ...
                        }
                        function2() {
                        var thisFile = 'file1.js';
                        // ...
                        }
                        function3() {
                        var thisFile = 'file1.js';
                        // ...
                        }

                        Now every function in the file knows the filename.

                        The bottom line is there is simply no way to do this. The client-side
                        JavaScript has no way to know what file it exists in, at run or any
                        other time.

                        As for Netscape 4. So I guess IE 3 and 4 and Opera 4, 5, and 6 should
                        never have been created either because they don't support functionality
                        modern browsers support. How about Mozilla 1.0.1? Should it have never
                        been created? It's newer than Netscape 4, but it lacks some
                        functionality and contains bugs which makes writing scripts that support
                        both it and modern versions of Mozilla problematic.

                        --
                        Grant Wagner <gwagner@agrico reunited.com>
                        comp.lang.javas cript FAQ - http://jibbering.com/faq


                        Comment

                        • Dr John Stockton

                          #13
                          Re: How to get the Javascript file name from a function inside the file.

                          JRS: In article <W3Fwd.27$3d3.9 1@news2.mts.net >, dated Fri, 17 Dec 2004
                          17:55:02, seen in news:comp.lang. javascript, Grant Wagner
                          <gwagner@agrico reunited.com> posted :[color=blue]
                          >
                          >The bottom line is there is simply no way to do this. The client-side
                          >JavaScript has no way to know what file it exists in, at run or any
                          >other time.[/color]

                          When the file is constructed, the constructing process will be aware of
                          the file name. The process may be manual, or automated.

                          It is merely necessary to use a constructing stage that copies the file
                          name from the outside of the file to the inside of the file. Details
                          depend on the system being used.


                          If DOS batch is available, consider a batch file containing

                          copy draft.js %1.js
                          mtr -x+ -n %1.js ItsMe.* = "ItsMe='%1. js'"

                          partly tested; mtr is MiniTrue. That should convert (the only) line
                          from var ItsMe=''
                          to var ItsMe='filename .js'

                          I assume the file is not subject to arbitrary subsequent renaming.


                          A different approach would be to compute the line
                          <script src="javascript .js"></script>
                          in which case code should know its contents.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                          Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                          I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
                          free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/> Update hope?

                          Comment

                          • Ryan Hubbard

                            #14
                            Re: How to get the Javascript file name from a function inside the file.


                            [color=blue]
                            > First, the following is the current standard:
                            ><script type='text/javascript' >src='somejavas cript.js'></script>[/color]
                            I am aware of the standards and thank you for pointing them out anyway
                            but this post is about something different.

                            [color=blue]
                            > Second, the following is not a true statement:
                            >"At runtime any code in this javascript.js file will be
                            > executed."
                            >It may declare functions that may or may not be executed.[/color]
                            Instead of depating semantics and getting really technical about an
                            issue that the post is not about how about we address the issue at hand.
                            Once again I am not a beginner.
                            [color=blue]
                            > Third, what's wrong with JRS' suggestion of just placing
                            > a variable in the
                            > file that identifies the name of the file?[/color]
                            Once again as in one of the above statements. This is a templating
                            system. The point is to Automate retreiving the file name. If we hard
                            coded it it would not be automated would it.

                            The point is if we call a function which resides in a javascript file.
                            Can that function determine what file it came from. I really can't
                            state it more clearly then that. The point is to automate this so hard
                            coding variables are out. The code must retrieve the file name through
                            the javascript API or core functions. It must be as cross browser as
                            possible.

                            Thanks in advance for anyone that can even say if it is possible.

                            *** Sent via Developersdex http://www.developersdex.com ***
                            Don't just participate in USENET...get rewarded for it!

                            Comment

                            • Tim Williams

                              #15
                              Re: How to get the Javascript file name from a function inside the file.

                              Ryan,

                              You don't seem to be having much luck finding an answer to your
                              original question. Perhaps if you were to explain why you need to do
                              this you might get some suggestions for alternatives.

                              Regards,
                              Tim.


                              "Ryan Hubbard" <ryanmhuc@yahoo .com> wrote in message
                              news:41c6f14a$1 _1@127.0.0.1...[color=blue]
                              >
                              >[color=green]
                              >> First, the following is the current standard:
                              >><script type='text/javascript' >src='somejavas cript.js'></script>[/color]
                              > I am aware of the standards and thank you for pointing them out
                              > anyway
                              > but this post is about something different.
                              >
                              >[color=green]
                              >> Second, the following is not a true statement:
                              >>"At runtime any code in this javascript.js file will be
                              >> executed."
                              >>It may declare functions that may or may not be executed.[/color]
                              > Instead of depating semantics and getting really technical about an
                              > issue that the post is not about how about we address the issue at
                              > hand.
                              > Once again I am not a beginner.
                              >[color=green]
                              >> Third, what's wrong with JRS' suggestion of just placing
                              >> a variable in the
                              >> file that identifies the name of the file?[/color]
                              > Once again as in one of the above statements. This is a templating
                              > system. The point is to Automate retreiving the file name. If we
                              > hard
                              > coded it it would not be automated would it.
                              >
                              > The point is if we call a function which resides in a javascript
                              > file.
                              > Can that function determine what file it came from. I really can't
                              > state it more clearly then that. The point is to automate this so
                              > hard
                              > coding variables are out. The code must retrieve the file name
                              > through
                              > the javascript API or core functions. It must be as cross browser as
                              > possible.
                              >
                              > Thanks in advance for anyone that can even say if it is possible.
                              >
                              > *** Sent via Developersdex http://www.developersdex.com ***
                              > Don't just participate in USENET...get rewarded for it![/color]


                              Comment

                              Working...