问题
Today it is common to use //server.tld/js/script.js
instead of the protocol-specific http://server.tld/js/script.js
in script
-Tags etc.
But I want to serve a static html page without any webserver (i.e. no http://localhost:8080
).
The problem is that the browser interprets the protocol-relative link as file:///js/script.js
and I can't circumvent this because it dynamically loads from a external server.
In this specific case, the script is http://connect.soundcloud.com/sdk.js
and loads //connect.soundcloud.com/soundmanager2/soundmanager2.js
at runtime.
Including a script
-Tag pointing to http://connect.soundcloud.com/soundmanager2/soundmanager2.js
directly in the html page doesn't solve the problem because there are some other dependencies, too.
There is also no way to get a webserver running (e.g. php -S localhost:8080
), because it's for a client who doesn't wish to install anything.
So how can I define in the html file that procotol-relative links should use http://
or https://
as protocol instead of file://
?
EDIT: Maybe I explain this with an example
In my html page I have a script tag (where I can control the used protocol)
<script src='http://example.org/some.js'></script>
And the script adds another tag in the dom
<script src='//example.org/helper.js'></script>
If I just open the html file file:///.../file.html
the browser thinks it should prepend file://
so the script src
becomes file://example.org/helper.js
or similar.
And it is not possible to just loop through the script tags and prepend a http://
, because the script (http://example.org/some.js
) might use ajax.
EDIT:
I don't think there is a way to set the protocol for 3rd-party-scripts if they use protocol-relative urls, so I leave this question as a reminder for someone who might encounter the same behaviour while working with 3rd-party-scripts and static html pages.
Would be nice if there would be a preinstalled webserver in Windows like in Linux' Python.
来源:https://stackoverflow.com/questions/19965985/protocol-relative-links-in-static-html-pages