gpt4 book ai didi

javascript - 如果用户没有将 http 添加到他们的链接中

转载 作者:行者123 更新时间:2023-12-03 02:04:32 24 4
gpt4 key购买 nike

我有一个网站,用户可以在其中分享他们的社交媒体帐户。但如果他们不添加 http,链接就会显示到 www.mysite.com/www.theirsite.com,然后重定向回我的网站。

<a href="{{channel.facebook_page}}" target="_blank" rel="nofollow"  title="{{channel.channel_name}} On FaceBook"><i class="fa fa-facebook" aria-hidden="true" style='color:#ee6f00'></i></a>

我知道大多数人会说只需将 http 作为帖子添加到链接中,例如 href="http://{{channel.facebook_page}}

但我不能这样做,因为有些人添加了它,有些人则没有。我有很多表单,其中的输入用于这些字段,并且不希望更改所有表单的麻烦。

我可以在显示链接上做些什么来解决这个问题。

最佳答案

那么你只需测试用户是否添加了它,如果没有,则添加它:

function fixHTTP(url) {
var regex = /^https{0,1}:\/\/.*/;
return regex.exec(url) ? url : ('http://' + url);
}

// use it like:
var url1 = 'www.google.es';
var url2 = 'https://www.google.es';
fixHTTP(url1); // returns 'http://www.google.es'
fixHTTP(url2); // returns 'https://www.google.es'

关于javascript - 如果用户没有将 http 添加到他们的链接中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49859872/

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