Relative Protocol with JS files

Testing with IE7 we have seen incorrect handling of relative protocols in a URL when the URL pointing to javascript files. The following works....

<SCRIPT language='JavaScript' type='text/javascript' SRC='http://foo.com/bar.js'></SCRIPT>

But the following does not work

<SCRIPT language='JavaScript' type='text/javascript' SRC='//foo.com/bar.js'></SCRIPT>

This works on all major browsers including prior versions of IE. HELP!!!



Answer this question

Relative Protocol with JS files

  • TonyV

    What build of IE7 are you using Such a bug was present in early pre-beta code, but this page:

    http://www.fiddlertool.com/test/protreluri.htm

    ...works just fine in IE7 RC1, and the referenced script is downloaded and executed.

    If you can still reproduce this bug in RC1, please provide a live repro URI.


  • pavel989

    Hi there,
    Tell me, why would you need to reference a javascript file this way Why not give the exact location of it instead For example, using the http:// in the referencing link, or, using /bar.js to reference it in the public_html directory. I am unsure as to why anybody would want to use something other than the solutions listed above.


  • arieltk

    A little more information we found out.

    Relative protocol is only working with the standard port numbers. Out initial testing was using servers running on non-standard ports, for example 8080 for HTTP.

    Below are the steps to re-create the problem:

    1) Setup an HTTP server to process responses from port 8080.

    2) Create a file called test.js with the following content:

    alert('ok');

    3) Create an html page that has a script tag to download the test.js file using relative protocol:

    <SCRIPT language='JavaScript' type='text/javascript' SRC='//mydomain.com:8080/test.js'></SCRIPT>

    4) Load the html page created in step 3 in IE 7, this will not result in the ‘ok’ alert dialog. Load the same html page in IE6 and the ‘ok’ alert dialog will appear.


  • BlueMikey

    The reason to reference a JScript (or image, or css, etc) file using a protocol-relative hyperlink is to make a page that works correctly when referenced by either HTTP or HTTPS. By making the URIs protocol-relative, you can ensure that a secure page downloads scripts using HTTPS (and hence avoids security prompts) while an insecure page downloads scripts using HTTP.


  • Relative Protocol with JS files