Why is 'http:' appended to a string combination that only appears to start with '//' at the beginning of a string?

Asked 2 years ago, Updated 2 years ago, 75 views

I am a beginner at JavaScript.I am also reading click.js to study JavaScript.
I'm going to read the click site while debugging on chrome.

http://kenwheeler.github.io/slick/

While I was looking at , on line 1041 (as of February 21, 2017),

dsq.src='//'+disqus_shortname+'.disqus.com/embed.js';

with the code running until just before it (with breakpoint configured),

dsq.src is ",
disqus_shortnameis 'slickcarousel'

Verify that
and then perform the steps (with chrome) Immediately after checking the value of dsq.src,

"http://slickcarousel.disqus.com/embed.js"

It was stated that

Here's the question.

Why does this leading "http:" come with it?

javascript url

2022-09-29 21:57

2 Answers

Is the code here?

varddisqus_shortname='slickcarousel';

(function(){
    vardsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src='//'+disqus_shortname+'.disqus.com/embed.js';
    ( document.getElementsByTagName('head')[0]|| document.getElementsByTagName('body')[0]).appendChild(dsq);
})();

First of all, the format of the URL is called protocol-relative URL for the format that starts with // without the protocol (http: or https:), and it is a companion of so-called "relative paths."Correct (valid) URI specified in the URI specification (per rfc2396,3986).
 The main use of is to write (technique) that both https and http connectivity protocols to web servers can prevent security errors in either case (although there is an error), for example, accessing resources in different domains is not recommended (Reference).
 In any case, if you use a URL in this format, the agent (client/browser) wants the absolute path from the relative path to actually throw the request, so in this case, the protocol is supplemented.
 You are setting the src property of HTMLScriptElement.Following the specification, HTMLScriptElementThe SCRIPT element>Attribute definitions srcBasic HTML data types>6.4URIs:

Relative URIs are resolved to full URIs using a base URI. [RFC1808], section 3, define the normal algorithm for this process.

Therefore, the value passed to the property expands to the absolute path, so reading it will contain the protocol complementary value (absolute path string).

To put it bluntly,

const str='//'+disqus_shortname+'.disqus.com/embed.js';
console.assert(str==='//slickcarousel.disqus.com/embed.js');

const str_assign=dsq.src=str;
console.assert(str_assign==='//slickcarousel.disqus.com/embed.js');

console.assert(dsq.src!=='//slickcarousel.disqus.com/embed.js');

That's right.


2022-09-29 21:57

If you omit the http: or https: URI schemes, the browser specifications complement the scheme.
Also, omitting the scheme is recommended by Google and others.
http://qiita.com/Sugima/items/785644372397595644ba

<!-- Deprecated-->
<script src="http://www.google.com/js/gweb/analytics/autotrack.js"></script>

<!--Recommended-->
<script src="//www.google.com/js/gweb/analytics/autotrack.js"></script>


2022-09-29 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.