You need to enable JavaScript to run this app. window.location.pathname.split('/')返回纯文本而非真实路径如何解决
优惠活动
大模型
产品
解决方案
定价
更多

window.location.pathname.split('/')返回纯文本而非真实路径如何解决

问题原因

  • 你将window.location.pathname.split('/')[1]这段需要执行的JS表达式直接写在了字符串引号内部,JS引擎会将其识别为普通文本拼接,不会执行计算得到真实路径值。

修复方案

把动态表达式移出字符串,用字符串拼接符+和前后的HTML字符串拼接即可,修正后的代码如下:

<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>

额外注意事项

  • 如果路径分段可能包含<、>、&等特殊字符,建议先对值做HTML转义再插入,避免出现XSS安全问题或者页面渲染异常。

内容的提问来源于stack exchange,提问作者Akirabyte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 09:15:03