location.pathname.split('/') 的问题

时间:2021-08-01 15:55:12

标签: javascript html

它返回 text "window.location.pathname.split('/')[1]" 而不是 REAL pathname,我该如何解决?

  <script>
     document.getElementById("categoria").innerHTML = 
     "<li style='display:inline;margin-left:5px;margin-right:5px;' class='navbithome'> <a href='index.php{vb:raw session.sessionurl_q}' accesskey='1'><img src='{vb:stylevar imgdir_misc}/navbit-home.png' alt='{vb:rawphrase home}' /></a></li>" + "<a href='javascript:history.back()'>window.location.pathname.split('/')[1]</a>";
  </script>

1 个答案:

答案 0 :(得分:0)

您必须将 window.location.pathname.split('/')[1] 与您分配给 document.getElementById("categoria").innerHTML 的字符串连接起来。

参见 this question 了解如何连接字符串和变量。在下面的代码段中,我使用了 template strings

document.getElementById("categoria").innerHTML =
  `<li style='display:inline;margin-left:5px;margin-right:5px;' class='navbithome'>
    <a href='index.php{vb:raw session.sessionurl_q}' accesskey='1'>
      <img src='{vb:stylevar imgdir_misc}/navbit-home.png' alt='{vb:rawphrase home}' />
    </a>
  </li>
  <a href='javascript:history.back()'>${window.location.pathname.split('/')[1]}</a>`;
<ul id="categoria"></ul>