简体   繁体   English

纯JavaScript中基于URL的单页应用导航?

[英]URL-based single page app nav in pure JavaScript?

I'm trying to write a single-page web app in JavaScript. 我正在尝试使用JavaScript编写单页Web应用程序。 Desired behavior: 所需行为:

  • When the user goes to http://myapp.example.com (the main page), or any other URL off of that domain for the first time, their browser downloads 1 large HTML file (and associated JS/CSS/image files, etc.). 当用户首次访问http://myapp.example.com (主页)或该域之外的任何其他URL时,他们的浏览器会下载1个大型HTML文件(以及相关的JS / CSS /图像文件,等等。)。 --> so big upfront download -> 这么大的前期下载
  • The HTML file contains <div> elements that each represent a different "page"; HTML文件包含<div>元素,每个元素代表一个不同的“页面”。 only 1 "page" div will be displayed at any given time 在任何给定时间仅显示1个“页面” div
  • When the user clicks a link that should bring them to a different "page", JS manipulates the DOM to hide the current page and show/display the desired one 当用户单击将其带到另一个“页面”的链接时,JS操纵DOM来隐藏当前页面并显示/显示所需的页面。
  • Every time the user "navigates" to a different "page", the URL in their browser should change, and the new URL should be added to browser history via HTML5 History API 每次用户“导航”到另一个“页面”时,其浏览器中的URL均应更改,并且应通过HTML5 History API将新的URL添加到浏览器历史记录中

I'm wondering how to do this. 我想知道如何做到这一点。 I'd greatly prefer something jQuery-based rather than trying to write something homegrown. 我非常喜欢基于jQuery的内容,而不是尝试编写本地化的内容。 But if I have to write this code myself, then given the following general structure of the 1 HTML file: 但是,如果我必须自己编写此代码,则可以使用以下1 HTML文件的一般结构:

<html>
    <head> ... </head>
    <body>
        <div id="home" class="page">
            <!-- Content for home page, say http://myapp.example.com/#home. -->
        </div>

        <div id="about" class="page">
            <!-- Content for About page, say http://myapp.example.com/#about. -->
        </div>

        <div id="contact" class="page">
            <!-- Content for Contact page, say http://myapp.example.com/#contact. -->
        </div>

        <!-- Etc. for all other "pages" -->         
    </body>
</html>

How do I: 我如何:

  • Hide all "page divs" by default, and then inspect the URL to figure out which "page" to show/display? 默认情况下,隐藏所有“页面div”,然后检查URL以找出要显示/显示的“页面”?
  • Add the current URL to the browser history via the HTML5 History API? 是否通过HTML5历史记录API将当前URL添加到浏览器历史记录中?

You can use and set the window.location.hash and use that value to $("#yourDivId").show() and $("#yourDivId").hide() your divs. 您可以使用并设置window.location.hash并将该值用于$("#yourDivId").show()$("#yourDivId").hide()

On the onload you can $("#yourDivId").show() the requested (or default if none was requested) div, and $("#yourDivId").hide() the other divs. onload您可以$("#yourDivId").show()所请求的div(如果没有请求, $("#yourDivId").hide()默认值),而$("#yourDivId").hide()是其他div。

And on each link, either directly change the hash and set the requested div visible. 并且在每个链接上,直接更改哈希值并将请求的div设置为可见。 Or set a timer that checks the hash on an interval to see if the hash has changed. 或设置一个计时器,以检查某个时间间隔上的哈希值以查看哈希值是否已更改。 (There is no native onhashchange event.) (没有本地onhashchange事件。)

I think you should hide every div at start. 我认为您应该在开始时隐藏每个div。

$(div#page).hide();

Then, you can check the current URl by using: 然后,您可以使用以下方法检查当前的UR1:

var pathname = window.location.pathname;

Every time the user changes the URL: first hide every div, then check and show the desired div. 每次用户更改URL时:首先隐藏每个div,然后检查并显示所需的div。

That is the reason of Single Page Application Framework's existence. 这就是单页应用程序框架存在的原因。 I think it does worth learning some of the, such as AngularJS or BackboneJS. 我认为值得学习其中的一些,例如AngularJS或BackboneJS。

If you want to abuse the browser history, I suggest you read this article on history.pushState() . 如果您想滥用浏览器历史记录,建议您阅读一下 history.pushState()上的这篇文章 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript 单一导航页面 - javascript single naV page 使按钮发送电子邮件,重定向页面并触发基于URL的下载 - Make button send email, redirect page, and trigger a url-based download 在(无框架)基于 Javascript 的单页应用程序中,URL queryString 更新,但脚本无法解析更新后的 URL - In a (frameworkless) Javascript-based Single Page App, the URL queryString updates but the script fails to parse the updated URL single.page.nav javascript无法正常工作 - single.page.nav javascript not working 纯JavaScript应用程序中的页面和令牌管理 - Page and token management in a pure JavaScript app 使用AngularJS及其RouteProvider的纯本地单页应用程序 - Pure Local Single-Page App With AngularJS and its RouteProvider 如何从客户端上传基于 URL 的 mp3 文件到节点服务器 - How to upload URL-based mp3 file from the client to the node server 使用纯 javascript 在滚动端进行页面捕捉(适用于 Cordova 应用程序) - Page snapping on scroll-end with pure javascript (for Cordova App) 根据 url 在单个页面上加载数据 - Load data on a single page based on the url 如何使用浏览器历史记录状态在我的单页JavaScript应用程序中加载URL? - How to load a URL in my single page JavaScript app using Browser history state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM