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
  • Lee

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

    Ryan Hubbard said:
    [color=blue]
    >The point is if we call a function which resides in a javascript file.
    >Can that function determine what file it came from.[/color]

    You cannot call a function that resides in a javascript file.
    That's not just a semantical quibble. It's central to your problem.

    At the time the page was loading, the function source was loaded from a
    particular javascript file, but at the time that the function executes, that
    relationship no longer exists. It is simply one of many functions loaded in the
    current page.

    In some browsers you may be able to search the contents of scripts that were
    loaded to find the source for a function of the same name, but that doesn't even
    ensure that you've found the source for the function that's executing.

    Comment

    • Grant Wagner

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

      "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
      news:18SxzyBjZ1 wBFwLm@merlyn.d emon.co.uk...[color=blue]
      > JRS: In article <W3Fwd.27$3d3.9 1@news2.mts.net >, dated Fri, 17 Dec[/color]
      2004[color=blue]
      > 17:55:02, seen in news:comp.lang. javascript, Grant Wagner
      > <gwagner@agrico reunited.com> posted :[color=green]
      > >
      > >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[/color]
      of[color=blue]
      > the file name. The process may be manual, or automated.
      >
      > It is merely necessary to use a constructing stage that copies the[/color]
      file[color=blue]
      > 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.[/color]

      Sure, the constructing process knows the name of the file. The
      client-side JavaScript contained in the file does not know the name of
      the file it resides in.

      As you have pointed out, this information could be included in the
      client-side JavaScript in a way that would make the client-side
      JavaScript "aware" of the name of the file it resides in.
      [color=blue]
      > A different approach would be to compute the line
      > <script src="javascript .js"></script>
      > in which case code should know its contents.[/color]

      Without external/server-side processing of some kind, I do not see how
      client-side JavaScript can "know" what file it resides in. I don't
      understand how your suggestion could be implemented using only
      client-side technologies.

      The OP has rejected the idea of inserting a variable assignment into
      each file that contains the name of the file the variable assignment is
      contained in. So either he does not understand that this process could
      be automated by an external/server-side process, or perhaps he wants a
      solution independant of server-side processing.

      If he does not understand the process could be automated using
      external/server-side processes, then perhaps he is not up to the
      challenge, or he is simply uninformed. Either way I'm not going to do
      his work for him.

      If he demands a client-side only solution, the simple answer is as
      already stated. There isn't one.

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


      Comment

      • codeHanger@yahoo.ca

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

        Grant Wagner wrote:[color=blue]
        > "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
        > news:18SxzyBjZ1 wBFwLm@merlyn.d emon.co.uk...[color=green]
        > > JRS: In article <W3Fwd.27$3d3.9 1@news2.mts.net >, dated Fri, 17 Dec[/color]
        > 2004[color=green]
        > > 17:55:02, seen in news:comp.lang. javascript, Grant Wagner
        > > <gwagner@agrico reunited.com> posted :[color=darkred]
        > > >
        > > >The bottom line is there is simply no way to do this. The[/color][/color][/color]
        client-side[color=blue][color=green][color=darkred]
        > > >JavaScript has no way to know what file it exists in, at run or[/color][/color][/color]
        any[color=blue][color=green][color=darkred]
        > > >other time.[/color]
        > >[/color][/color]
        <...>[color=blue][color=green]
        > > A different approach would be to compute the line
        > > <script src="javascript .js"></script>
        > > in which case code should know its contents.[/color]
        >
        > Without external/server-side processing of some kind, I do not see[/color]
        how[color=blue]
        > client-side JavaScript can "know" what file it resides in. I don't
        > understand how your suggestion could be implemented using only
        > client-side technologies.
        >
        > The OP has rejected the idea of inserting a variable assignment into
        > each file that contains the name of the file the variable assignment[/color]
        is[color=blue]
        > contained in. So either he does not understand that this process[/color]
        could[color=blue]
        > be automated by an external/server-side process, or perhaps he wants[/color]
        a[color=blue]
        > solution independant of server-side processing.
        >
        > If he does not understand the process could be automated using
        > external/server-side processes, then perhaps he is not up to the
        > challenge, or he is simply uninformed. Either way I'm not going to do
        > his work for him.
        >
        > If he demands a client-side only solution, the simple answer is as
        > already stated. There isn't one.
        >[/color]

        Hmmm --- that seems a bit categorical. It's not necessarily so, in
        principle.

        Regards,

        ../rh

        Comment

        • Jay Den

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

          This will work with any browser with the document.script s object and the
          document.getEle mentsByTagName. This is but is not limited to Mozilla
          1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the currect
          browsers used on the net according to currect publishings.

          The document.script s object loads a script object before the file is
          included. Therefore any code which will execute at runtime can find
          it's name. Use that to set the variable.

          ---- Code inside JS file -----------------------------
          var scripts = document.getEle mentsByTagName( "SCRIPT")
          var script = scripts[scripts.length-1].src

          // Now depending on the browser the src could be
          // just the file or the absolute URL so just parse it
          var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)

          // Now script Name contains the name of the script

          ------------------------------------------------------

          The issue is, which nobody has brought up, is namespace. The above
          automates retrieving the name which a function can then access but
          multiple files using this code will overwrite the scriptName variable.
          Each of my js files is a class (obviously each class is uniquely named).
          I store the script name in a static class variable. This works perfect
          for me.

          Hope this helps.



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

          Comment

          • codeHanger@yahoo.ca

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

            Jay Den wrote:[color=blue]
            > This will work with any browser with the document.script s object and[/color]
            the[color=blue]
            > document.getEle mentsByTagName. This is but is not limited to Mozilla
            > 1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the[/color]
            currect[color=blue]
            > browsers used on the net according to currect publishings.
            >[/color]
            [Unfortunately, since you appear to not consistently follow newgroup
            protocol (i.e., provide attribution/quoted text), I don't know whether
            this is simply your musings on the topic, or whether this is directed
            to my earlier post.]

            There are relatively frequent warnings in this newsgroup regarding
            lies, damned lies, and the above. The methodology used to collect
            browser demographics is usually highly flawed.
            [color=blue]
            > The document.script s object loads a script object before the file is
            > included. Therefore any code which will execute at runtime can find
            > it's name. Use that to set the variable.
            >
            > ---- Code inside JS file -----------------------------
            > var scripts = document.getEle mentsByTagName( "SCRIPT")
            > var script = scripts[scripts.length-1].src
            >
            > // Now depending on the browser the src could be
            > // just the file or the absolute URL so just parse it
            > var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)
            >
            > // Now script Name contains the name of the script
            >
            > ------------------------------------------------------[/color]

            It sounds like you've taken an empirical approach which may or may not
            work reliably.
            [color=blue]
            >
            > The issue is, which nobody has brought up, is namespace. The above
            > automates retrieving the name which a function can then access but
            > multiple files using this code will overwrite the scriptName[/color]
            variable.[color=blue]
            > Each of my js files is a class (obviously each class is uniquely[/color]
            named).[color=blue]
            > I store the script name in a static class variable. This works[/color]
            perfect[color=blue]
            > for me.
            >[/color]

            Since you appear to be looking for a universal solution, perhaps Grant
            is right that there isn't one.

            If you wish to use something that may work in a number of browsers
            (e.g., Firefox and Mozilla cousins, and Opera), consider loading the
            library components dynamically and, with some care, monitor the changes
            that occur in the global object as each loading occurs. That should
            provide you with a relationship between the file being loaded, and the
            global functions contained within.
            [color=blue]
            > Hope this helps.[/color]
            Likewise.

            Regards,
            ../rh

            Comment

            • jaden10001@yahoo.com

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


              codeHanger@yaho o.ca wrote:[color=blue]
              > Jay Den wrote:[color=green]
              > > This will work with any browser with the document.script s object[/color][/color]
              and[color=blue]
              > the[color=green]
              > > document.getEle mentsByTagName. This is but is not limited to[/color][/color]
              Mozilla[color=blue][color=green]
              > > 1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the[/color]
              > currect[color=green]
              > > browsers used on the net according to currect publishings.
              > >[/color]
              > [Unfortunately, since you appear to not consistently follow newgroup
              > protocol (i.e., provide attribution/quoted text), I don't know[/color]
              whether[color=blue]
              > this is simply your musings on the topic, or whether this is directed
              > to my earlier post.]
              >
              > There are relatively frequent warnings in this newsgroup regarding
              > lies, damned lies, and the above. The methodology used to collect
              > browser demographics is usually highly flawed.
              >[/color]
              I apologize I should have stated where my statistics where taken from.
              The rough stat I posted above was taken from the last six months
              records from Nielsen//Netratings. If fortune 500 companies make
              decisions based on this information it is very likely that the op can
              as well.
              [color=blue][color=green]
              > > The document.script s object loads a script object before the file[/color][/color]
              is[color=blue][color=green]
              > > included. Therefore any code which will execute at runtime can[/color][/color]
              find[color=blue][color=green]
              > > it's name. Use that to set the variable.
              > >
              > > ---- Code inside JS file -----------------------------
              > > var scripts = document.getEle mentsByTagName( "SCRIPT")
              > > var script = scripts[scripts.length-1].src
              > >
              > > // Now depending on the browser the src could be
              > > // just the file or the absolute URL so just parse it
              > > var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)
              > >
              > > // Now script Name contains the name of the script
              > >
              > > ------------------------------------------------------[/color]
              >
              > It sounds like you've taken an empirical approach which may or may[/color]
              not[color=blue]
              > work reliably.
              >[color=green]
              > >
              > > The issue is, which nobody has brought up, is namespace. The above
              > > automates retrieving the name which a function can then access but
              > > multiple files using this code will overwrite the scriptName[/color]
              > variable.[color=green]
              > > Each of my js files is a class (obviously each class is uniquely[/color]
              > named).[color=green]
              > > I store the script name in a static class variable. This works[/color]
              > perfect[color=green]
              > > for me.
              > >[/color]
              >
              > Since you appear to be looking for a universal solution, perhaps[/color]
              Grant[color=blue]
              > is right that there isn't one.
              >
              > If you wish to use something that may work in a number of browsers
              > (e.g., Firefox and Mozilla cousins, and Opera), consider loading the
              > library components dynamically and, with some care, monitor the[/color]
              changes[color=blue]
              > that occur in the global object as each loading occurs. That should
              > provide you with a relationship between the file being loaded, and[/color]
              the[color=blue]
              > global functions contained within.
              >[color=green]
              > > Hope this helps.[/color]
              > Likewise.
              >
              > Regards,
              > ../rh[/color]

              Comment

              • ryanmhuc@yahoo.com

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

                Jay,
                Thanks for the idea. I tested it out and it works great. It worked
                in Opera 6 and 7, Netscape 6 and up, IE 5 and 6 and testing using
                Mozilla 1.0 and 1.7. That is good enough for me. Thanks again for the
                idea I was starting to think that it was not possible.

                Ryan

                Jay Den wrote:[color=blue]
                > This will work with any browser with the document.script s object and[/color]
                the[color=blue]
                > document.getEle mentsByTagName. This is but is not limited to Mozilla
                > 1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the[/color]
                currect[color=blue]
                > browsers used on the net according to currect publishings.
                >
                > The document.script s object loads a script object before the file is
                > included. Therefore any code which will execute at runtime can find
                > it's name. Use that to set the variable.
                >
                > ---- Code inside JS file -----------------------------
                > var scripts = document.getEle mentsByTagName( "SCRIPT")
                > var script = scripts[scripts.length-1].src
                >
                > // Now depending on the browser the src could be
                > // just the file or the absolute URL so just parse it
                > var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)
                >
                > // Now script Name contains the name of the script
                >
                > ------------------------------------------------------
                >
                > The issue is, which nobody has brought up, is namespace. The above
                > automates retrieving the name which a function can then access but
                > multiple files using this code will overwrite the scriptName[/color]
                variable.[color=blue]
                > Each of my js files is a class (obviously each class is uniquely[/color]
                named).[color=blue]
                > I store the script name in a static class variable. This works[/color]
                perfect[color=blue]
                > for me.
                >
                > Hope this helps.
                >
                >
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                > Don't just participate in USENET...get rewarded for it![/color]

                Comment

                Working...