【问题标题】:How can I make my html website display the page route that I am on?如何让我的 html 网站显示我所在的页面路径?
【发布时间】:2021-11-10 01:23:18
【问题描述】:

我想让我的 html 网站显示我所在的当前页面的路线,如下所示: 网站: www.example.com/example 内容: 您访问了示例页面 网站: www.example.com/page 内容: 您访问过的页面页面

我知道如何使用烧瓶来做到这一点,但我将如何使用纯 html 或 JavaScript 来做到这一点?

【问题讨论】:

    标签: html url routes


    【解决方案1】:

    您可以在窗口对象上使用位置 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>

    【讨论】:

    • 有没有办法让它在 html 页面上显示,“抱歉,{route} 不存在”,但如果它是一个有效的路由,它会转到正确的页面并且不会t显示那个?这是一个示例: URL:mysite.com/h8dshfds 页面内容:对不起,没有名为 h8dshfds 的页面。
    • 我在回答中编辑了 sn-p 以最符合您的要求。
    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多