1

What is the recommended way for linking to external CSS files and JavaScript files on a HTTPS secure webpage, e.g is it better to use:

//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css 

or

https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css
  • Your question is almost clear. ;-) Do you mean using HTTP versus HTTPS? Looking at your link examples, the first one is incomplete. I would think you would want to use HTTPS from an HTTPS page but that assumes that HTTPS is an option with CloudFlare. Otherwise, using HTTP could cause a notice in the user browser each time they access your site. – closetnoc Dec 6 '15 at 1:22
  • Kind of, What I meant was is it better to explicitly specify the protocol as HTTPS or is it better to use protocol relative links? My Site forces HTTPS anyway but I was just curious which is the best method to use – itteam Dec 6 '15 at 2:16
  • Okay. I got it. I have not idea. Sorry. – closetnoc Dec 6 '15 at 2:53
  • If your site forces HTTPS then there is zero difference between the two options. – Tim Fountain Dec 6 '15 at 15:49
1

If the page is on HTTPS, and will only ever be accessed over HTTPS then it doesn't make any difference. (Well, the scheme-relative URL is 6 fewer bytes before compression).

If the page will sometimes be accessed over HTTP and sometimes over HTTPS then using a scheme relative URL will make it work consistently.

If the page will sometimes be accessed over HTTPS and sometimes as a local file, then a scheme relative URL will break (because file://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css doesn't exist).

There isn't a "best", just different priorities.

0

For compatibility sake, I'd recommend the full absolute URL (second URL) in your case. I'm not sure how many browsers support protocol-less URLs but I bet some older browsers like netscape won't understand protocol-less URLs.

If you're trying to save the number of bytes on a page used for linking to external resources, then have your resources stem from the same folder, then add the following between the <head> and </head> of your HTML:

<base href="http://example.com/path/to/all/assets/">

The value for href is the base folder where all your resources are stored plus a trailing slash.

Then if all assets are in the exact same folder, you can easily call them by filename such as:

<img src="somefile.jpg" width=100 height=200 alt="a picture">

<script src="somescript.js">
</script>

If all the above HTML is used unmodified in the same webpage, then the image requested will be:

http://example.com/path/to/all/assets/somefile.jpg

and the javascript file to load will be:

http://example.com/path/to/all/assets/somefile.js

This is because the base tag sets the default folder for the resources on the page.

  • 2
    "I'm not sure how many browsers support protocol-less URLs" - it has been a standard for over 20 years, so I'd guess the answer is 'all of them'. – Tim Fountain Dec 6 '15 at 15:48
  • That's interesting. Protocol-less URL's seems to work for me in netscape too. Normally being absolute is always better, especially when the time available for testing HTML is extremely limited. – Mike Dec 7 '15 at 1:00
  • Thanks for the suggestion, I decided for the sake of compatibility to just add a PHP constant that contained cdnjs.cloudflare.com/ajax/libs so in the PHP pages I can just do <?php echo CDNJS_PATH; ?> for consistency across all externally linked resources. – itteam Dec 29 '15 at 15:04
0

Use Github its best for this. You can store css and javascript files and there is no http or https issue.

0

Most browsers block script from unsafe sources. We have seen this issue while changed http to https so, we had to change source of scripts from http to https sources

for example:

http://code.jquery.com/jquery-2.1.4.min.js

changed to

https://code.jquery.com/jquery-2.1.4.min.js 

similar modification done for all website content sources(script,css,images)

for https site, all content should be loaded from https sources.

  • This does not actually answer the question... which is preferable, a "protocol relative" or "absolute" URL? – MrWhite Jan 5 '16 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.