简体   繁体   English

使用javascript在单页应用程序中获取以前的网站网址

[英]Get previous site url in a Single Page Application using javascript

I'm working on a vue single page application. 我正在处理vue单页应用程序。

Situation : I have a page which displays 1..* results (Page A), if there is only 1 result available, then this result is automatically selected; 情况 :我有一个页面显示1..*结果(页面A),如果只有1个结果可用,那么将自动选择此结果; meaning the user is redirected to the items detail page (Page B). 表示将用户重定向到商品详细信息页面(页面B)。

[Page A] <-> [Page B]

Problem : if the user is on Page B and navigates back then the 'auto-select'-clause is triggered again which results in a redirect again to Page B. This means you cannot navigate back once you've entered Page B - this is unacceptable. 问题 :如果用户位于页面B上并向后导航,则再次触发“自动选择”子句,这将导致再次重定向到页面B。这意味着一旦进入页面B,便无法向后导航-这是不可接受的。

Thoughts : The best solution I can think of is to add a check to the auto-select clause which matches the origin (where the user came from, eg Page B) to the desired destination (where to user is redirected to, eg also Page B), and if they do match, do not trigger the redirect. 想法 :我能想到的最佳解决方案是在自动选择子句中添加一个检查,该检查将原点(用户来自例如B页)与期望的目标(用户重定向到例如Page B),并且如果确实匹配,请不要触发重定向。 The problem is that I have no way of knowing where the user was before. 问题是我无法知道用户以前在哪里。

I tried to use document.referrer but this isn't suitable for a single page application. 我尝试使用document.referrer但这不适用于单页应用程序。

I tried to work with vue-router properties, but none of which brought me any futher. 我尝试使用vue-router属性,但是没有一个带给我更多的帮助。

The only other solution I can think of is to store each navigation in a cookie (or similar) to match against, but since this is quite a rare case I try to avoid having too much of an overhead. 我能想到的唯一其他解决方案是将每个导航存储在cookie(或类似文件)中以进行匹配,但是由于这种情况很少见,因此我尝试避免过多的开销。

Any idea? 任何想法? Is there something similar like document.referrer but for single page applications? 是否有类似document.referrer东西,但适用于单页应用程序? Or is there a way to extract information from the history ? 还是有办法从history提取信息?

When result is only 1, then PageA will redirected to PageB ,if in this case , let the route like "/PageB?only=1". 当结果仅为1时,则PageA将重定向到PageB,如果在这种情况下,让路由类似“ / PageB?only = 1”。 in pageB, if the query's only is 1, then when you click the back button,redirected the page that you want to back. 在pageB中,如果查询的唯一值为1,则当您单击“后退”按钮时,将重定向您要返回的页面。 I am not sure it is the best~ but it should work。 我不确定这是最好的,但是应该可以。

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

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