logo

What I've Learnt Already Today. And It's Only 9am!

Did you know that you can add an absolute off-server link to a resource like this:

<script src="//other.server.com/dir/file.js"></script>

No need for it to be:

<script src="http://other.server.com/dir/file.js"></script>

While I was semi-aware of this behaviour I never realised how it could be of use until I read Ned's post about it this morning. And I'm glad I did!

What use is it? Well, it seems that more and more of the web apps I work on are needing to be SSL-enabled. Some also need the option for non-SSL access. So the protocol in use could be either http or https. This isn't a problem until you start to add resources (Flash, images, CSS, scripts etc) from other servers.

If you include resources on a https page using the http protocol the user gets a big fat warning about it, which isn't good, so you need to make sure the protocols match! But, using the technique above, if you link to all these resources without the protocol the browser will just use the current pages protocol. No need for a computed "@If(HTTPS="ON"; "https"; "http")" value before all other resources!

As ever, it just goes to show — you think you know all the basics of your job and then...

Comments

  1. Jake,

    Excellent post; thanks so much. I was actually dealing with this very thing just yesterday! Good to know that the HTTPS check isn't necessary, and that will certainly simplify things for me. I'm heading off to read Ned's post right now.

    I read your blog every day, and I always appreciate your good information. Thanks for taking the time to continue your site!

    John

  2. Great Tip, Jake. More (undocumented?) goodness!

    This was a concern here not long ago when our production environment had https but dev did not. This would have been handy then.

    • avatar
    • Jake Howlett
    • Thu 18 Oct 2007 04:04 PM

    Thank Ned, not me. I'm just regurgitating it with a slight Domino angle.

  3. I used this technique a few months ago. It is all valid, standard complient and all that.

    The only downside is the crappy crawlers, that doesn't know what // means. The http log got littered with urls like:

    /other.server.com/dir/file.js

    on the this.server.com

    and they show up as error 404.

    Quite annoying, that this great trick makes the error count increase.

    Another use for this trick is offsite links:

    <a href="//other.server.com/">Link to other server</a>

    you save 5 chars. :-)

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 18 Oct 2007

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content