Scheme Agnostic Urls

September 28, 2011

Interesting fact I just discovered. It's possible to specify full urls that automatically pick http or https depending on how the current page was delivered. This helps when using CDNs.

Specify a double slash, rather than the whole prefix:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>

This will automatically pick from

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>

or

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>

References