Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to replace http:// with // in a <script src="http://...">?

I have the following element:

<script type="text/javascript" src="https://cdn.example.com/js_file.js"></script> 

In this case the site is HTTPS, but the site may also be just HTTP. (The JS file is on another domain.) I'm wondering if it's valid to do the following for convenience sake:

<script type="text/javascript" src="//cdn.example.com/js_file.js"></script> 

I'm wondering if it's valid to remove the http: or https:?

It seems to work everywhere I have tested, but are there any cases where it doesn't work?

like image 761
Darryl Hein Avatar asked Feb 15 '09 00:02

Darryl Hein


People also ask

What is the use of script src?

Definition and Usage The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.

Can you have multiple script src in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag.

Where should script src go?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.


1 Answers

A relative URL without a scheme (http: or https:) is valid, per RFC 3986: "Uniform Resource Identifier (URI): Generic Syntax", Section 4.2. If a client chokes on it, then it's the client's fault because they're not complying with the URI syntax specified in the RFC.

Your example is valid and should work. I've used that relative URL method myself on heavily trafficked sites and have had zero complaints. Also, we test our sites in Firefox, Safari, IE6, IE7 and Opera. These browsers all understand that URL format.

like image 73
Jeff Avatar answered Sep 16 '22 15:09

Jeff