javascript - 如何处理默认页面中的相对href
问题描述
我的应用程序托管使用 maven-dependency-plugin 下载的第三方 HTML 应用程序。默认页面是index.html
,因此可以加载为http://server/parent/path
或http://server/parent/path/index.html
。
默认页面使用相对地址(例如src="./somescript.js"
)加载样式表和脚本。这在 URL 为 时有效http://server/parent/path/index.html
,但在它只是 时失败http://server/parent/path
。服务器接收到请求/parent/somescript.js
并返回 404。
我可以index.html
在构建期间进行编辑,一种解决方案是插入一行脚本:
<script>if (!/index.html/i.test(window.location.href)) window.location.href += "/index.html";</script>
这将导航到index.html
,以便始终相对于该页面请求资源。这行得通,但感觉不对。
一个正统的解决方案可能是插入一个<base>
标签,但这很棘手,因为我在构建时不知道完整路径是什么——它可能是http://server/parent/path/
或http://server/application/parent/path/
. 我不能使用绝对基本路径,而相对路径会因为与上述相同的原因而失败。
任何人都可以提出更好的解决方案吗?
解决方案
推荐阅读
- ios - 如何在 UIPageViewController 中的视图控制器之前、之后删除?
- c# - 使用 nunit 报告 Specflow 测试的名称、步骤和状态
- reactjs - 是否可以在 React 项目的 scss 文件中使用 svg 文件?
- javascript - 打字稿:在时间戳上增加一小时
- php - laravel php str_replace函数用2个不同的值替换2个不同的值
- node.js - Firebase fcm sendMulticast 错误:“恰好需要主题、令牌或条件之一”
- oracle - Oracle 和 Postgres 之间的数据复制
- yii2 - yii2中的用户注册?
- laravel - 如何在 laravel 中从数据库中获取条带密钥并发布密钥
- javascript - express http服务器应该与rabbitmq消费者解耦吗?