Href 行为 - 需要澄清

7
在我的网站个人资料页面上有这样的代码。
<div class="col-md-4">
    <h2 class="textclass2" style="text-decoration: underline; margin-bottom: 10px"><%=playerdata.Player_Name%>   AKA  <%=playerdata.Player_NickName%></h2>
    <a class="btn btn-dark btn-xs" href="<%=playerdata.Player_FBLink%>" id="fbclick" onClick="checkUrl()" style="font-size: 12px; color:#40FF40; margin-bottom: 10px "> Facebook </a>
    <a class="btn btn-dark btn-xs" href="<%=playerdata.Player_InstagramLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Instagram </a>
    <a class="btn btn-dark btn-xs" href="<%=playerdata.Player_LinkedinProfile%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> LinkedIN </a>
    <a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>

这是使用 EJS 完成的。它们基本上是 href 标签,其中包含用户社交页面的 URL 字符串。后端是 MongoDB。

问题 - 如果我在数据库中的相应字段中输入有效的 URL,则会被重定向到正确的 URL。但是如果字符串为空,则将发布 GET 请求到 /profile/:username。

这是 href 标签通常的行为方式吗?如果是,有什么方法可以规避吗?基本上,如果字符串为空,我不想执行 GET 调用,而只是向用户闪现一个消息,告诉他们该字符串为空。


1
你可以不渲染 href <%=playerdata.Player_FBLink ? 'href="' + playerdata.Player_FBLink + '"' : ''%> - Xesenix
一个空的href只是链接到当前位置。 - Gershom Maes
其中一个 URL 在 FB 上有 onClick checkUrl(),这一定会调用此函数。从代码行为来看,空 URL 将始终指向同一页,请分享更多代码或解释更多。可能还有一个内部函数在之前被调用...您需要调试代码以查看代码执行的流程。 - Learning
1个回答

4
如果没有 href 属性,<a> 标签会表现得就像 <span> 一样。如果 href 属性为空,则它将继承浏览器地址栏中的 URL。所以,如果没有 Twitter 链接,一种选择是根本不显示该链接。
另一个选项是使用 href:script 来显示某些警报。
  <% if (playerdata.Player_TwitterLin) { %>
    <a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
  <% } else { %>
  <a class="btn btn-dark btn-xs" href="javascript:alert('Twitter link not available')" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
  <% } %>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接