简体   繁体   English

如何知道一个网站是否是单页应用程序?

[英]How to know if a website is a single page application?

I have extremely little experience with web tech, only know basic HTML and CSS.我对网络技术的经验非常少,只知道基本的 HTML 和 CSS。 I have an assignment where I'm supposed to evaluate a website and identify web techs that can help improve the site.我有一个任务,我应该评估一个网站并确定可以帮助改进网站的网络技术。 One of the first things I want to figure out is whether the one I've chosen is a multi or single page application.我想弄清楚的第一件事是我选择的应用程序是多页应用程序还是单页应用程序。 I've been googling for hours for code that's 'typical' for SPA, but haven't found anything other than this:我一直在谷歌上搜索 SPA 的“典型”代码数小时,但除此之外没有找到任何其他内容:

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html">
<title>Single Page Sliding Layout - Design Shack Demo</title>
<meta name="author" content="Jake Rocheleau">
<link rel="shortcut icon" href="https://designshack.net/favicon.ico">
<link rel="icon" href="https://designshack.net/favicon.ico">
<link rel="stylesheet" type="text/css" media="all" href="css/styles.css">
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/jquery.scrollTo.js"></script>

Does this indicate that it's a SPA?这是否表明它是一个SPA? Or am I completely off track?还是我完全偏离了轨道? This is the website if that helps: http://www.mrbottles.com/如果有帮助,这是网站: http : //www.mrbottles.com/

Thanks!谢谢!

/Person who is about to pull all their hair out /即将拔掉所有头发的人

SPAs - Single Page Applications SPA - 单页应用程序

Precursor前体

One thing to get out of the way: there are Single Page Applications and Single Page Websites .有一件事要避开:有单页应用程序单页网站 There is a difference, although they both can have similarities.有区别,尽管它们都可以有相似之处。

A Single Page Website is a website that tries to put all the content on one page, you normally scroll through the page to get to the other sections and often there is a sticky navigation that follows you and tells you which section you're on.单页网站是一个尝试将所有内容放在一个页面上的网站,您通常滚动页面以到达其他部分,并且通常会有一个粘性导航跟随您并告诉您您所在的部分。 Also clicking on navigation is often an anchor link that will keep you on the page and jump you to the section without reloading (this can be done without javascript).此外,点击导航通常是一个锚链接,可以让您留在页面上并跳转到该部分而无需重新加载(这可以在没有 javascript 的情况下完成)。

A Single Page Application is often seen in two flavors when page interaction or navigation occurs.当页面交互或导航发生时,单页面应用程序通常有两种风格。 One flavor is that the URL will have a # in it which the contents behind the # character change upon interactions.一种风格是 URL 中会有一个##字符后面的内容会在交互时发生变化。 This is similar to the Single Page Website when clicking an anchor, except content is normally dynamically loaded in rather than the page scrolling to a section of the page.这与单击锚点时的单页网站类似,不同之处在于内容通常是动态加载的,而不是页面滚动到页面的某个部分。 The other utilizes something called the browser history state.另一个利用称为浏览器历史状态的东西。 The javascript is able to change the page's path without reloading the window. javascript 能够在不重新加载窗口的情况下更改页面的路径。

Why do you care ?你为什么关心 What is your mission, what are you trying to accomplish?你的使命是什么,你想完成什么? Most people care about knowing if a page is an SPA because they're scoping the viability of whether or not their product offering will work with a client's website.大多数人关心知道一个页面是否是 SPA,因为他们正在确定他们的产品是否适用于客户网站的可行性。 Ultimately you want to know if content is dynamically loading in. If your offering modifies content on the page but content is changing there's no real great ways to fire your code off again upon SPA navigation/content change.最终,您想知道内容是否正在动态加载。如果您的产品修改了页面上的内容但内容正在发生变化,那么在 SPA 导航/内容更改时,没有真正的好方法可以再次触发您的代码。 Without knowing the reason, it makes it difficult to answer this question, but I will try to cover a wide scope.在不知道原因的情况下,很难回答这个问题,但我会尽量涵盖广泛的范围。

How do I detect an SPA?如何检测 SPA?

It wasn't really stated whether you're trying to manually detect or programmatically detect an SPA.并没有真正说明您是尝试手动检测还是以编程方式检测 SPA。 Unfortunately there are no flags that would be set and just because you find a framework doesn't mean that it is being utilized to make the page an SPA.不幸的是,没有设置标志,仅仅因为您找到一个框架并不意味着它被用来使页面成为 SPA。

Manually Detecting an SPA手动检测 SPA

The most straightforward method to detect an SPA is to manually check it.检测 SPA 的最直接方法是手动检查它。

Steps to Manually Detect SPA 手动检测 SPA 的步骤
  1. Open developer tools / inspector打开开发人员工具/检查器
  2. Reload the page or navigate to it重新加载页面或导航到它
  3. Notice the timeline/waterfall of the requests注意请求的时间线/瀑布
  4. Click on a link or other interaction you have in concern单击您关心的链接或其他互动
    • If the timeline refreshes and causes a page reload then it's not an SPA如果时间线刷新并导致页面重新加载,则它不是 SPA
    • If the timeline shows the first hits and additional hits after your interaction then you have an SPA.如果时间线显示互动后的第一次点击和其他点击,那么您就有了 SPA。
Programmatically Detect an SPA 以编程方式检测 SPA

This can be complicated because SPAs can hook events and even prevent them from bubbling up or propagating if you're trying to listen to determine if the page is an SPA.这可能很复杂,因为 SPA 可以挂钩事件,甚至在您尝试侦听以确定页面是否为 SPA 时阻止它们冒泡或传播。

Here are a few ideas I came up with to detect SPAs.以下是我为检测 SPA 提出的一些想法。


When the window navigates away you can determine that a reload is about to take place.当窗口导航离开时,您可以确定将要进行重新加载。 At this point you can say that the page is not an SPA.此时您可以说该页面不是 SPA。 If you interacted and expected the reload to take place but did not then you have an SPA.如果您进行了交互并期望重新加载发生但没有发生,那么您就有了 SPA。

 window.onbeforeunload = function(event) { var text = 'Not an SPA!'; event.returnValue = text; return text; };

  • You could hook/proxy the history state APIs to know if they were called.您可以挂钩/代理历史状态 API 以了解它们是否被调用。
  • You could watch the history state.你可以查看历史状态。 If a change occurs then you have an SPA如果发生变化,那么您就有了 SPA
  • You could come up with some crazy link/element click event watcher that delays for some arbitrary time and if the time passes and the timed event fires then it's probably an SPA otherwise the page navigated away and the event never finishes firing.你可以想出一些疯狂的链接/元素点击事件观察器,它会延迟一些任意时间,如果时间过去了并且定时事件触发,那么它可能是一个 SPA 否则页面导航离开并且事件永远不会完成触发。 This isn't great because you have to wait some arbitrary amount of time;这不是很好,因为你必须等待一些任意的时间; what if you didn't want long enough and the page was being slow and still navigates away?如果您不想要足够长的时间并且页面很慢并且仍然导航离开怎么办?

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

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