Href 行为 - 需要明确

San*_*air 6 javascript href ejs

我在我网站的个人资料页面上有这样的代码。

<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>
Run Code Online (Sandbox Code Playgroud)

这是使用 EJS 完成的。它们基本上是具有用户社交页面 URL 字符串的 href 标签。后端是一个MongoDB

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

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

Vin*_*ney 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>
  <% } %>
Run Code Online (Sandbox Code Playgroud)