Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery或Javascript将所有http://转换为https://_Javascript_Jquery - Fatal编程技术网

使用jQuery或Javascript将所有http://转换为https://

使用jQuery或Javascript将所有http://转换为https://,javascript,jquery,Javascript,Jquery,我正在尝试将src标记中的链接转换为https://。更准确地说,Youtube iframes。例如: <iframe width='650' height='365' src="http://www.youtube.com/embed/y9kBixgYKzw" frameborder="0" allowfullscreen></iframe> 有人能给我指出正确的方法吗?一般来说,下面的例子说明了它是如何工作的。按每个循环元素,并替换源标记,如代码中所示: $(

我正在尝试将src标记中的链接转换为
https://
。更准确地说,Youtube iframes。例如:

<iframe width='650' height='365' src="http://www.youtube.com/embed/y9kBixgYKzw" frameborder="0" allowfullscreen></iframe>


有人能给我指出正确的方法吗?

一般来说,下面的例子说明了它是如何工作的。按
每个
循环元素,并替换源标记,如代码中所示:

$("iframe").each(function() {
    $(this).attr("src", $(this).attr("src").replace("http://", "https://"));
});

但请记住,这可能太晚了。帧可能已经通过
http
加载。如前所述,您必须在服务器端执行此操作,以确保内容将通过
https

加载。您是否正在尝试更改iframe源中URL的协议?或者尝试将传入请求重定向到HTTPS对应方

如果是后者,您可能需要尝试以下解决方案:

你可以在头脑中运行这个Javascript

如果您试图更改iframe的来源,我相信您将遇到eisbehr解释的问题。也就是说,外部源已经通过HTTP加载。

尝试将“https://”替换为“/”

试试看


在JS中这样做太晚了,因为对
http://
URL的请求已经发出了。您需要直接在源代码中进行更改,或者在将HTML提供给客户端之前在服务器上进行更改首先使用
$("iframe").each(function() {
    $(this).attr("src", $(this).attr("src").replace("http://", "https://"));
});
if (window.location.protocol != "https:")
    window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
src="//www.youtube.com/embed/y9kBixgYKzw"
if (location.protocol != 'https:')
{
 location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
}