Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a URL starting with // valid?

Tags:

We can see many HTML page using src="//example.com/myjavascript.js" to include a .js and let the browser use http:// / https:// depending on the current scheme of the page URL.

Is this a valid URI ?

like image 438
toutpt Avatar asked Aug 09 '12 10:08

toutpt


People also ask

What is considered a valid URL?

A URL is a valid URL if at least one of the following conditions holds: The URL is a valid URI reference [RFC3986]. The URL is a valid IRI reference and it has no query component. [RFC3987]

How do I check if a URL is valid?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

What does please enter a valid URL mean?

The error message "LinkedIn URL must be a valid URL" indicates that your link does not include all the necessary components for the system to recognize it as a valid URL. You may be seeing this error message because your link may not have "https://" at the beginning.

What is a URL link?

A URL (Uniform Resource Locator) is a unique identifier used to locate a resource on the Internet. It is also referred to as a web address. URLs consist of multiple parts -- including a protocol and domain name -- that tell a web browser how and where to retrieve a resource.


2 Answers

Yes, it is definitely valid. It is a "scheme relative" or "protocol relative" URI. In the spec since the beginning. Quite helpful to deal with http/https issues.

Read a much better description and caveats: Is it valid to replace http:// with // in a <script src="http://...">?

A few points to keep in mind:

  • There are some minor issues on IE 7 & 8 with double downloads
  • If you are viewing an HTML page from a file the browser will replace the scheme with file://, and not load your JS file from the server like it would with a full URL starting with http:// or https://.

Edit for modern webdev practices:

While the URL is still valid, it is now recommended to use https for third party resources (and, serve those resources from secure pages). The performance or compatibility issues from years ago are largely resolved with updated protocols and browsers.

like image 136
Adam B Avatar answered Oct 27 '22 03:10

Adam B


As a warning... we encountered issues with this methodology NOT being supported in Internet Explorer when used inside the HTML BASE tag (IE8 and 9 confirmed, didn't test any others before patching the issue).

After newly implementing SSL on a website, we had initially updated the BASE tags across the application to simply reference "//www.mydomain.com" : but the page templates broke (in IE only) on all scripts which did NOT live at the root of the site. Makes sense, since the BASE tag was not picking up the stylesheet or other relatively-linked resources which assumed they were being referenced from root, not from a nested location.

Since the BASE URI was our own domain anyway, we fixed the issue by setting the BASE tag's value to "https://www.mydomain.com". No matter if the client is browsing the site in SSL mode or out, at least it will work cross-browser now and can ALWAYS find its content - and by forcing https at all times, we ensure we don't get any content-mismatch warnings in the process by setting the BASE path to an http location while browsing a page using https.

like image 27
Dave McHale Avatar answered Oct 27 '22 03:10

Dave McHale