我正在开发一个网站,该网站有一个基本滑块,其中有 4 张左右滑动的幻灯片,仅显示页面居中的 1 张幻灯片。所有幻灯片的内容都加载在一个页面上,每个页面都有自己的 div。
<div id="slider-box">
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
</div>
没什么花哨的。此外,每张幻灯片都与一个链接相关联:
<a href="/gallery">Gallery</a>
<a href="/links">Links</a>
<a href="/about">About</a>
<a href="/resources">Resources</a>
因此我开始使用 History.js 将每张幻灯片与其各自的链接链接起来; www.example.com/about。我可以毫无问题地改变状态;后退和前进工作以及 URL 更改。
我碰巧注意到的是,如果我使用诸如 www.example.com/about 之类的虚假 URL 加载页面,页面将返回 404。
我能做些什么来防止 404 发生,同时确保页面是否被带到正确的幻灯片?
我还应该这样做吗?
我处于包含 jQuery 和 HTML5 的 LAMP 堆栈中。
我真的很困惑这个......谢谢!
I'm working on a site that has a basic slider with 4 slides that go left and right that only shows 1 slide centered in the page. All the slides' content are loaded on a single page each within it's own div.
<div id="slider-box">
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
<div class="slide">HTML and bg image</div>
</div>
Nothing fancy. Also each slide is associated with a link:
<a href="/gallery">Gallery</a>
<a href="/links">Links</a>
<a href="/about">About</a>
<a href="/resources">Resources</a>
So I started using History.js to link up each slide with it's respective link; www.example.com/about. I can change the state with no issue; back and forward work and the URL changes.
What I did happen to notice is that if I load the page with a false URL like, www.example.com/about, the page returns a 404.
What can I do to prevent the 404 from occurring, but also making sure the page is taken to the correct slide?
Is there another way I should be doing this?
I'm in a LAMP stack with jQuery and HTML5.
I'm really stuck on this one...thanks!
发布评论
评论(2)
如果我理解正确的话,您实际上必须创建
/about
、/gallery
等页面。想一想 – History API 只能在单个页面中工作。它会操纵地址栏显示的内容,但不会自行创建实际的 URL。我的解决方案是使用 mod_rewrite 之类的东西。
/about
会重定向到类似/?p=about
的内容,它会加载已打开的相应div
的页面。其他幻灯片也一样。只有这样您才能使用 History API 的功能。If I'm understanding you correctly, you actually have to create the
/about
,/gallery
, etc. pages. Think about it – the History API only works within a single page. It manipulates what the address bar says, but it doesn't create actual URLs on its own.My solution would be using something like mod_rewrite.
/about
would redirect to something like/?p=about
, which loads your page with the correspondingdiv
already open. Same for other slides. Only then you can use the History API's functionality.我们的 Web 服务器负责处理 HTTP 404 错误,请参阅您的 Web 服务器文档,了解如何使用重定向等创建自定义错误页面。如果您使用的是 Microsoft Internet Information Server 7,请查看此处 http://blogs.iis.net/rakkimk/archive/2008/10/03/iis7-enabling-custom-error-pages.aspx
Its our web server that handles the HTTP 404 error, see your web server documentation for how to create custom error pages with redirects etc. If you are using Microsoft Internet Information Server 7 then look here http://blogs.iis.net/rakkimk/archive/2008/10/03/iis7-enabling-custom-error-pages.aspx