简体   繁体   中英

page.js make examples/hash work with hashbang: true

The question says it all. I'm trying to make the example under examples/hash/ in the page.js examples work the same way it does, only using the { hashbang: true } option, but no avail.

I've also tried setting <base href="/hash/"> , but that seems to get into an infinite redirection. After that, removing page.base('/hash') seems to do the trick, but then the location bar displays http://localhost:4000/hash/#!/hash/ and the # and #subsection links stop working properly.

This is the code:

<!DOCTYPE html>
<html>
  <head>
    <title>Hash</title>
    <style>
      body p {
        color: #333;
        font-family: verdana;
      }
      #sections p {
        height: 500px;
        margin: 30px;
        padding: 30px;
        line-height: 40px;
        background-color: #eee;
        border: 1px solid #ccb;
        font-size: 30px;
      }

      #sections p a {
        display: block;
        float: right;
        font-size: 14px;
      }

    </style>
    <script src="/page.js"></script>
  </head>
  <body>
    <h1 id="top">Hash</h1>

    <ul>
      <li><a href="#">#</a></li>
      <li><a href="#subsection">#subsection</a></li>
      <li><a href="section?name=tana">section?name=tana</a></li>
      <li><a href="section?name=tana#subsection">section?name=tana#subsection</a></li>
    </ul>

    <div id="sections">
      <p><strong>A</strong><a href="#top">top</a></p>
      <p><strong>B</strong><a href="#top">top</a></p>
      <p id="subsection"><strong>C</strong><a href="#top">top</a></p>
    </div>

    <script>
      page.base('/hash');
      page('/:section', section);
      page();

      function section(ctx, next) {
        console.log('path: ', ctx.path);
        console.log('querystring: ', ctx.querystring);
        console.log('hash: ', ctx.hash);
        console.log(' ');
      }
    </script>
  </body>
</html>

How can I do this ? Thanks

I'm having the same problem and couldn't find a solution. What I ended up doing was use a hacky workaround.

You can push the correct version of your url using history.pushState if you don't have to support old browsers.

history.pushState('','',location.pathname + location.search);

This cosmetically fixes the problem, but deep linking to complex paths will require a bit more logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM