gpt4 book ai didi

javascript - 将我网站中的所有链接更改为 https

转载 作者:可可西里 更新时间:2023-11-01 17:06:33 27 4
gpt4 key购买 nike

我在 blogger 中的站点,域名为 https://www.arwikig.com/如您所见,我使用 ssl,并且每个想法都很好,但在帖子中我看到错误“相同的内容未保存在此站点中”并且我在我的站点中看到太多链接在我的站点中使用 http .. 即使在我的 html 中所有链接都是“//”,这意味着 https .. & 我尝试使用这个 js。

        <script type='text/javascript'>
//<![CDATA[
function RedirNonHttps() {
if (location.href.indexOf("https://") == -1) {
location.href = location.href.replace("http://", "https://");
}
}
//]]>
</script>

& 太多其他像这样的脚本& 我看不到任何效果谢谢你帮助我,我很抱歉我的英语不好

最佳答案

好吧,我想你执行的功能对吗?如果你没有,你可以让它自动执行此操作:

<!--This awesome script -->
<script type='text/javascript'>
//<![CDATA[
(function RedirNonHttps() {
if (location.href.indexOf("https://") == -1)
{
location.href = location.href.replace("http://", "https://");
}
})();
//]]>
</script>

这种语法被称为“自调用函数”,有a link

更新 #1嗯,我在你的网站上测试这段代码,改变 'http'在每个href , srccontent属性。

// 1. let's find those bad guys who are annoying us
// [href*='http:'] => Will find those with attribute href containing the string http
// [src*='http:'] => Will find those with attribute src containing the string http
// [content*='http:'] => Will find those with attribute content containing the string http

$("[href*='http:'], [src*='http:'], [content*='http:']").each(function(){

// Let's save the reference of matched item
var element = $(this);

// 2. Get the attributes for the current matched element, it could has href,
// src or even content attributes, and finally replace them
var href = (element.attr("href") || "").replace("http://", "https://");
var src = (element.attr("src") || "").replace("http://", "https://");
var content = (element.attr("content") || "").replace("http://", "https://");

// 3. Now just update the new attributes with the fresh and sweet data
element.attr("href" , href);
element.attr("src" , src);
element.attr("content", content);
});

希望对你有所帮助。但我仍然认为你应该在你的代码中手动编写它。

哦,顺便说一句!要调用它,您应该将其包装在 $(document).ready() 中或 $(function(){}) ,因为 jQuery 需要整个页面被完全加载才能工作:

// Common way... 
$(document).ready(function(){
// - - The code that I wrote before
});

// Or you can use this way! (Pretty short and funny)
$(function(){
// - - The code that I wrote before
});

关于javascript - 将我网站中的所有链接更改为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com