SRC parameter not working in Mozilla when path is included

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wylbur37

    #1

    SRC parameter not working in Mozilla when path is included

    (This is a re-post of something that apparently did not post correctly
    the first time, so if there's more than one copy, please ignore the
    duplicate).

    I normally use Mozilla 1.4 on Windows XP Pro.
    As I was developing some test webpages, I discovered that the SRC
    parameter doesn't seem to work when a path is used with the filename.

    In one example, when I want to run a JavaScript from an external file
    (i.e., not inline), I would do the following ...

    <SCRIPT LANGUAGE="JavaS cript" SRC="program.js "> </SCRIPT>

    If "program.js " were in a different subdirectory than the .htm file
    referring to it, the filename would have to be prefixed by the
    pathname as follows ...

    <SCRIPT LANGUAGE="JavaS cript" SRC="/mydir/program.js"> </SCRIPT>

    But when I ran the above from within an .htm file loaded locally into
    Mozilla, it did not work. (The result was as if the above code weren't
    there). However, when I ran the identical code on Internet Explorer
    (v.6), it worked.


    In another, even simpler example, the code to put an image on a
    webpage is ...

    <img src="picture.jp g">

    Again, if "picture.jp g were in a different subdirectory than the .htm
    file referring to it, the filename would have to be prefixed by the
    pathname as follows ...

    <img src="/mydir/picture.jpg">

    When I ran the above from within an .htm file loaded locally into
    Mozilla, it did not work. (The result was as if the above code weren't
    there). However, when I ran the identical code on Internet Explorer
    (v.6), it worked.


    By the way, I tried using backslashes instead of forward slashes. In
    both cases, it still worked on Internet Explorer but not on Mozilla.


    The SRC parameter seems like a fairly frequently used parameter for
    the developers of Mozilla to overlook it. Is this really a bug in
    Mozilla or is it just some option/switch in Mozilla that I didn't set
    properly?


    _______________ _______________ ____
    Do you Yahoo!?
    Find out what made the Top Yahoo! Searches of 2003

  • Lee

    #2
    Re: SRC parameter not working in Mozilla when path is included

    wylbur37 said:
    [color=blue]
    >The SRC parameter seems like a fairly frequently used parameter for
    >the developers of Mozilla to overlook it. Is this really a bug in
    >Mozilla or is it just some option/switch in Mozilla that I didn't set
    >properly?[/color]

    Neither.

    An URL beginning with a "/" is invalid. Since it isn't relative to
    the URL of the current page, you must specify the protocol. IE lets
    you get away with it, assuming that you mean the "file:" protocol.

    There are many reasons why it's bad for a browser to make that sort
    of assumption, but they are balanced by the fact that accepting bad
    code will trick some people into thinking that the other browsers
    are buggy, and so they will be less likely to support them, giving
    the more lenient browsers a market advantage.

    The correct tag would be:

    <script type="text/javascript" src="file:///C:/mydir/program.js">

    You should see that format in the Location bar when you open any
    local HTML file.

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: SRC parameter not working in Mozilla when path is included

      Lee <REM0VElbspamtr ap@cox.net> writes:
      [color=blue]
      > An URL beginning with a "/" is invalid. Since it isn't relative to
      > the URL of the current page, you must specify the protocol. IE lets
      > you get away with it, assuming that you mean the "file:" protocol.[/color]

      It is not invalid in general. As an URI fragment, it defines an
      absolute path, relative to the current protocol and server. It is
      correct for IE to assume the "file" protocol and "localhost"
      server. Where it gets dicey is what happens on local files.

      If the URL to the local file is
      file://localhost/c:/somedire/myhtml/foo.html
      then using the URI fragment in that context "/mydir/program.js" should
      give
      file://localhost/mydir/program.js
      That is most likely not what is wanted. For one thing, it doesn't include
      a drive letter. I guess it is what Mozilla does (ok, let me check ... yes,
      it gives
      file:///mydir/program.js
      which is equivalent - omitting the server name in a file protocol means
      the server is localhost).

      If you compare it to IE, the same test gives:
      file:///D:/mydir/program.js
      (my test file was on the D: drive). That is incorrect according to
      the definition of URIs, but obviously works better on windows based
      machines.

      Mozilla also runs on, e.g., Unix machines, where there are no drive
      letters, so I can see why the want to keep the code general.
      [color=blue]
      > There are many reasons why it's bad for a browser to make that sort
      > of assumption, but they are balanced by the fact that accepting bad
      > code will trick some people into thinking that the other browsers
      > are buggy, and so they will be less likely to support them, giving
      > the more lenient browsers a market advantage.[/color]

      Hear, hear!
      [color=blue]
      > <script type="text/javascript" src="file:///C:/mydir/program.js">[/color]

      The problem is that you need to change the file before uploading
      to a server. The advantage is that it works locally :)

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Michael Winter

        #4
        Re: SRC parameter not working in Mozilla when path is included

        On 2 Jan 2004 07:02:37 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
        [color=blue]
        > An URL beginning with a "/" is invalid. Since it isn't relative to
        > the URL of the current page, you must specify the protocol. IE lets
        > you get away with it, assuming that you mean the "file:" protocol.[/color]

        That statement is incorrect as long as the document has a base URL; either
        specified explicitly or derived from the document (it's file: or http:
        URL). A URL beginning with a single forward slash would be parsed like so:

        base: http://www.example.com/html/main.html

        abs: /mydir/picture.jpeg -> http://www.example.com/mydir/picture.jpeg

        If 'mydir' was a subdirectory in 'html', the relative URL should be:

        rel: ./mydir/picture.jpeg ->


        OR:

        rel: mydir/picture.jpeg

        You were correct though that a URL beginning with a forward slash is not
        relative.

        Using the file: scheme, the results should be similar: a single slash
        would begin from the root directory of the current drive. However, RFC
        1808 doesn't give examples for the file: scheme (probably due to numerous
        and dissimilar filesystem formats).

        <snip>

        Mike


        RFC 1808: http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1808.html

        There is a list of examples towards the end of the document
        (Section 5.1 - Normal Examples) that include a single, initial
        forward slash.

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Michael Winter

          #5
          Re: SRC parameter not working in Mozilla when path is included

          On Fri, 02 Jan 2004 17:37:04 +0100, Lasse Reichstein Nielsen
          <lrn@hotpop.com > wrote:
          [color=blue][color=green]
          >> <script type="text/javascript" src="file:///C:/mydir/program.js">[/color]
          >
          > The problem is that you need to change the file before uploading
          > to a server. The advantage is that it works locally :)[/color]

          However, the simple thing is to mirror the directory structure on the
          server on the development machine(s) as well (it's what I do). If you only
          use relative URLs, with '../' used as necessary, you need to change
          nothing, and it works locally, too. Of course, you need to plan your
          directory structures carefully first, but shouldn't you always do that?

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Christian Eyrich

            #6
            Re: SRC parameter not working in Mozilla when path is included

            wylbur37 wrote:
            [color=blue]
            > I normally use Mozilla 1.4 on Windows XP Pro.
            > As I was developing some test webpages, I discovered that the SRC
            > parameter doesn't seem to work when a path is used with the filename.
            >
            > In one example, when I want to run a JavaScript from an external file
            > (i.e., not inline), I would do the following ...
            >
            > <SCRIPT LANGUAGE="JavaS cript" SRC="program.js "> </SCRIPT>
            >
            > If "program.js " were in a different subdirectory than the .htm file
            > referring to it, the filename would have to be prefixed by the
            > pathname as follows ...
            >
            > <SCRIPT LANGUAGE="JavaS cript" SRC="/mydir/program.js"> </SCRIPT>
            >
            > But when I ran the above from within an .htm file loaded locally into
            > Mozilla, it did not work. (The result was as if the above code weren't
            > there). However, when I ran the identical code on Internet Explorer
            > (v.6), it worked.[/color]

            "/mydir/program.js" is an absolute path. On your server this is ok if
            the url is "www.yoursite.c om/mydir/program.js". But on the local machine
            mydir is searched in the root dir - wherever this may be on Windows
            machines (not C: at least on Win98).
            Opera shows same results and I guess that's right.

            Christian

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: SRC parameter not working in Mozilla when path is included

              Michael Winter <M.Winter@bluey onder.co.invali d> writes:
              [color=blue]
              > However, the simple thing is to mirror the directory structure on the
              > server on the development machine(s) as well (it's what I do). If you
              > only use relative URLs, with '../' used as necessary, you need to
              > change nothing, and it works locally, too.[/color]

              Yes, as long as the paths are in relation to the HTML file, it is
              simple. However, if your javascript files contain paths as well, it
              becomes difficult. E.g., this code in an external file:

              function makeMyImg() {
              var img = new Image();
              img.src = "../images/myImage.png";
              return img;
              }

              The problem is that when that code is called from an HTML page, the path
              is taken relative to the HTML file, not the Javascript file. If you have
              HTML files in different levels of directories, no relative path will
              work for all of them. That makes absolute paths necessary for Javascript
              files that are shared between different pages.
              [color=blue]
              > Of course, you need to plan your directory structures carefully
              > first, but shouldn't you always do that?[/color]

              Absolutely. And always sooner than you expect :)

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • kaeli

                #8
                Re: SRC parameter not working in Mozilla when path is included

                In article <hdze8bzi.fsf@h otpop.com>, lrn@hotpop.com enlightened us
                with...[color=blue]
                > The problem is that when that code is called from an HTML page, the path
                > is taken relative to the HTML file, not the Javascript file. If you have
                > HTML files in different levels of directories, no relative path will
                > work for all of them. That makes absolute paths necessary for Javascript
                > files that are shared between different pages.
                >[/color]

                Actually, I always use relative paths because of portability, and I had
                to write a workaround to just this problem.
                What I did was basically check where I was and write the source element
                dynamically.

                It was a server-side solution (coldfusion), but I bet it could be done
                client-side as well, if needed.

                --
                --
                ~kaeli~
                Condoms should be used on every conceivable occasion.



                Comment

                • Grant Wagner

                  #9
                  Re: SRC parameter not working in Mozilla when path is included

                  Lasse Reichstein Nielsen wrote:
                  [color=blue][color=green]
                  > > <script type="text/javascript" src="file:///C:/mydir/program.js">[/color]
                  >
                  > The problem is that you need to change the file before uploading
                  > to a server. The advantage is that it works locally :)[/color]

                  The solution is to install and run an httpd on the development PC, then you are
                  accessing the site using the same protocol and file paths as you will on the
                  real server.

                  --
                  | Grant Wagner <gwagner@agrico reunited.com>

                  * Client-side Javascript and Netscape 4 DOM Reference available at:
                  *


                  * Internet Explorer DOM Reference available at:
                  *
                  http://msdn.microsoft.com/workshop/a...ence_entry.asp

                  * Netscape 6/7 DOM Reference available at:
                  * http://www.mozilla.org/docs/dom/domref/
                  * Tips for upgrading JavaScript for Netscape 7 / Mozilla
                  * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                  Comment

                  • Richard Cornford

                    #10
                    Re: SRC parameter not working in Mozilla when path is included

                    "Christian Eyrich" <VJBDONFBFOPG@s pammotel.com> wrote in message
                    news:bt4akc$36s df$1@ID-205057.news.uni-berlin.de...
                    <snip>[color=blue]
                    >... But on the local machine mydir is searched in the
                    >root dir - wherever this may be on Windows machines
                    >(not C: at least on Win98).[/color]
                    <snip>

                    If you look at a file protocol URL on a Windows machine, such as -
                    file:///C:/mydir/program.js
                    - you may notice that the "file:" part is separated from the rest of
                    the URL by three slashes instead of the two that would usually be in
                    that location. The last of those three slashes represents the root
                    directory and as that is above the level of the drive letter it clearly
                    cannot represent any real location in the file system of a windows
                    machine. IE browsers seem to confuse the issue by applying the same path
                    rules that the OS uses when resolving file names.

                    Richard.



                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: SRC parameter not working in Mozilla when path is included

                      "Richard Cornford" <Richard@litote s.demon.co.uk> writes:
                      [color=blue]
                      > If you look at a file protocol URL on a Windows machine, such as -
                      > file:///C:/mydir/program.js
                      > - you may notice that the "file:" part is separated from the rest of
                      > the URL by three slashes instead of the two that would usually be in
                      > that location. The last of those three slashes represents the root
                      > directory[/color]

                      No. It separates the server from the path. In file URL's the server
                      can be omitted if it is "localhost" , but the above is equivalent to
                      the URI
                      file://localhost/C:/mydir/program.js
                      The usual rules apply, so this can be separated using the grammar
                      productions (RFC 2396 section 3):

                      absoluteURI :: scheme ":" hier_part
                      hier_part :: net_path
                      net_path :: "//" authority [abs_path]
                      abs_path :: "/" path_segments

                      Here, scheme is "file", authority is the "localhost" server and
                      path_segments is "C:/mydir/program.js".

                      There is no initial slash in the path segments, but when the
                      scheme+server is specified, the path is necessarily absolute.

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Dr John Stockton

                        #12
                        Re: SRC parameter not working in Mozilla when path is included

                        JRS: In article <hdze8bzi.fsf@h otpop.com>, seen in
                        news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                        posted at Fri, 2 Jan 2004 19:02:57 :-[color=blue]
                        >
                        > function makeMyImg() {
                        > var img = new Image();
                        > img.src = "../images/myImage.png";
                        > return img;
                        > }
                        >
                        >The problem is that when that code is called from an HTML page, the path
                        >is taken relative to the HTML file, not the Javascript file. If you have
                        >HTML files in different levels of directories, no relative path will
                        >work for all of them. That makes absolute paths necessary for Javascript
                        >files that are shared between different pages.[/color]


                        When a page is being written, one should know its position on the site.

                        So surely the function should be along the lines of

                        function makeMyImg(PartP ath) {
                        var img = new Image();
                        img.src = PartPath + "images/myImage.png";
                        return img;
                        }

                        or even have the full name of the image passed as a parameter?

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: SRC parameter not working in Mozilla when path is included

                          Lasse Reichstein Nielsen wrote:
                          [color=blue]
                          > The problem is that when that code is called from an HTML page, the path
                          > is taken relative to the HTML file, not the Javascript file. If you have
                          > HTML files in different levels of directories, no relative path will
                          > work for all of them. That makes absolute paths necessary for Javascript
                          > files that are shared between different pages.[/color]

                          It does not. In the linked script file you can retrieve the URL of
                          the linking document, as for example (from my box of olde things ;-)):

                          function CalcPaths() { /* Calculates relative image file paths to
                          absolute ones */
                          var PathEnd = document.URL.la stIndexOf('\\') ; // Folder path
                          if (PathEnd == -1)
                          PathEnd = document.URL.la stIndexOf('/'); // URL
                          if (PathEnd == -1) { // Extends filename to full path
                          var prevImgCollapse d = imgCollapsed;
                          imgCollapsed = document.URL.sl ice(0, PathEnd);
                          imgCollapsed += "/";
                          imgCollapsed += prevImgCollapse d;
                          var prevImgExpanded = imgExpanded;
                          imgExpanded = document.URL.sl ice(0, PathEnd);
                          imgExpanded += "/";
                          imgExpanded += prevImgExpanded ;
                          }
                          }


                          PointedEars

                          Comment

                          • Michael C.

                            #14
                            Re: SRC parameter not working in Mozilla when path is included

                            On Fri, 2 Jan 2004 06:11:14 -0800 (PST),
                            wylbur37 <wylbur37nospam @yahoo.com> wrote:[color=blue]
                            > (This is a re-post of something that apparently did not post correctly
                            > the first time, so if there's more than one copy, please ignore the
                            > duplicate).
                            >
                            > I normally use Mozilla 1.4 on Windows XP Pro.
                            > As I was developing some test webpages, I discovered that the SRC
                            > parameter doesn't seem to work when a path is used with the filename.
                            >
                            > In one example, when I want to run a JavaScript from an external file
                            > (i.e., not inline), I would do the following ...
                            >
                            > <SCRIPT LANGUAGE="JavaS cript" SRC="program.js "> </SCRIPT>
                            >
                            > If "program.js " were in a different subdirectory than the .htm file
                            > referring to it, the filename would have to be prefixed by the
                            > pathname as follows ...
                            >
                            > <SCRIPT LANGUAGE="JavaS cript" SRC="/mydir/program.js"> </SCRIPT>
                            >[/color]

                            Try

                            <SCRIPT LANGUAGE="JavaS cript" SRC="file:///mydir/program.js">
                            </SCRIPT>

                            or

                            <SCRIPT LANGUAGE="JavaS cript"
                            SRC="http://my.server.org/mydir/program.js">
                            </SCRIPT>

                            AFAIK, you don't assume a server. Just because it works in IE, doesn't
                            make it correct. And don't use backslashes, IE might not balk at them,
                            but if you ever want to view the files in anything but IE, you'll be
                            disappointed.

                            HTH,

                            Michael C.
                            --
                            mcsuper5@usol.c om http://mcsuper5.freeshell.org/

                            If we cease to judge this world, we may find ourselves, very quickly, in
                            one which is infinitely worse. - Margaret Atwood

                            Comment

                            Working...