我正在尝试将src标记中的链接转换为https://
。更准确地说,是Youtube iframes。例如:
<iframe width='650' height='365' src="http://www.youtube.com/embed/y9kBixgYKzw" frameborder="0" allowfullscreen></iframe>
我尝试了下面的代码,但没有成功:
function RedirNonHttps() {
if (location.src.indexOf("https://") == -1) {
location.src = location.src.replace("http://", "https://");
}
}
<body onload="RedirNonHttps();">
有人能告诉我做这件事的正确方法吗?
发布于 2016-07-19 16:57:01
一般来说,下面的例子展示了它是如何工作的。通过each
循环元素并替换each
标记,如代码中所示:
$("iframe").each(function() {
$(this).attr("src", $(this).attr("src").replace("http://", "https://"));
});
但请记住,这可能为时已晚。这些帧可能已经通过http
加载。如前所述,您必须在服务器端执行此操作,以确保内容将通过https
加载。
发布于 2016-07-19 17:03:56
您是否正在尝试更改iframe源中URL的协议?或者尝试将传入的请求重定向到HTTPS对等项?
如果是后者,您可能希望尝试这样的解决方案:Detect HTTP or HTTPS then force HTTPS in JavaScript
if (window.location.protocol != "https:")
window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
你可以在你的Javascript中运行它。
如果你试图改变iframe的来源,我相信你会遇到eisbehr已经解释过的问题。也就是说,外部源已经通过HTTP加载。
发布于 2019-08-28 01:55:52
尝试将"https://“”替换为“just "//”
src="//www.youtube.com/embed/y9kBixgYKzw"
https://stackoverflow.com/questions/38453746
复制相似问题