【发布时间】:2021-11-10 01:23:18
【问题描述】:
我想让我的 html 网站显示我所在的当前页面的路线,如下所示: 网站: www.example.com/example 内容: 您访问了示例页面 网站: www.example.com/page 内容: 您访问过的页面页面
我知道如何使用烧瓶来做到这一点,但我将如何使用纯 html 或 JavaScript 来做到这一点?
【问题讨论】:
我想让我的 html 网站显示我所在的当前页面的路线,如下所示: 网站: www.example.com/example 内容: 您访问了示例页面 网站: www.example.com/page 内容: 您访问过的页面页面
我知道如何使用烧瓶来做到这一点,但我将如何使用纯 html 或 JavaScript 来做到这一点?
【问题讨论】:
您可以在窗口对象上使用位置 API:
window.location.href
编辑:
下面的 sn-p 是我使用 vanilla javascript 的方式:
const paragraphEl = document.querySelector('.not-found-page p')
const url = window.location.href
const idx = url.search(/.net\//)
const route = url.substring(idx+5)
if (rest !== 'js') {
paragraphEl.innerText = `Sorry, the current route (${window.location.href}) is not available.`
} else {
window.location.href = 'https://website.com/some-other-route'
}
<section class="not-found-page">
<p></p>
</section>
【讨论】: