- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的单页应用程序使用 Sammy.js。我想创建类似于 SO 的功能(当您输入问题并尝试离开页面时,它会询问您是否确定)。
如果它不是单页应用,我会做类似的事情:
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
问题在于,在单页应用程序中,用户实际上并没有离开页面,而是更改了他的 document.location.hash
(他可以通过关闭页面来离开页面)。有没有办法为 SPA 制作类似的东西,最好是使用 sammy.js?
最佳答案
在我的工作中,我们在单页 Web 应用程序中遇到了类似的问题需要解决。我们有一些页面可能是脏的,如果它们是脏的,我们希望阻止导航离开该页面,直到用户确认可以这样做。由于我们想阻止导航,我们无法监听 onhashchange 事件,该事件在哈希值更改之后触发,而不是之前。因此,我们决定覆盖默认的 LocationProxy 以包含允许我们在位置更改之前有选择地阻止导航的逻辑。
考虑到这一点,这是我们使用的代理:
PreventableLocationProxy = (function () {
function PreventableLocationProxy(delegateProxy, navigationValidators) {
/// <summary>This is an implementation of a Sammy Location Proxy that allows cancelling of setting a location based on the validators passed in.</summary>
/// <param name="delegateProxy" type="Sammy.DefaultLocationProxy">The Location Proxy which we will delegate all method calls to.</param>
/// <param name="navigationValidators" type="Function" parameterArray="true" mayBeNull="true">One or more validator functions that will be called whenever someone tries to change the location.</param>
this.delegateProxy = delegateProxy;
this.navigationValidators = Array.prototype.slice.call(arguments, 1);
}
PreventableLocationProxy.prototype.bind = function () {
this.delegateProxy.bind();
};
PreventableLocationProxy.prototype.unbind = function () {
this.delegateProxy.unbind();
};
PreventableLocationProxy.prototype.getLocation = function () {
return this.delegateProxy.getLocation();
};
PreventableLocationProxy.prototype.setLocation = function (new_location) {
var doNavigation = true;
_.each(this.navigationValidators, function (navValidator) {
if (_.isFunction(navValidator)) {
// I don't just want to plug the result of the validator in, it could be anything!
var okayToNavigate = navValidator(new_location);
// A validator explicitly returning false should cancel the setLocation call. All other values will
// allow navigation still.
if (okayToNavigate === false) {
doNavigation = false;
}
}
});
if (doNavigation) {
return this.delegateProxy.setLocation(new_location);
}
};
return PreventableLocationProxy;
}());
这段代码本身非常简单,它是一个带有委托(delegate)代理以及一个或多个验证器函数的 javascript 对象。如果这些验证器中的任何一个显式返回 false,则导航将被阻止并且位置不会改变。否则,允许导航。为了使这项工作正常进行,我们必须覆盖 anchor 标记的默认 onclick 处理以通过 Sammy.Application.setLocation 对其进行路由。不过,一旦完成,这就可以让我们的应用程序干净利落地处理脏页逻辑。
为了更好地衡量,这是我们的脏页验证器:
function preventNavigationIfDirty(new_location) {
/// <summary>This is an implementation of a Sammy Location Proxy that allows cancelling of setting a location based on the validators passed in.</summary>
/// <param name="new_location" type="String">The location that will be navigated to.</param>
var currentPageModels = [];
var dirtyPageModels = [];
//-----
// Get the IDs of the current virtual page(s), if any exist.
currentPageModels = _.keys(our.namespace.currentPageModels);
// Iterate through all models on the current page, looking for any that are dirty and haven't had their changes abored.
_.forEach(currentPageModels, function (currentPage) {
if (currentPage.isDirty() && currentPage.cancelled === false) {
dirtyPageModels.push(currentPage);
}
});
// I only want to show a confirmation dialog if we actually have dirty pages that haven't been cancelled.
if (dirtyPageModels.length > 0) {
// Show a dialog with the buttons okay and cancel, and listen for the okay button's onclick event.
our.namespace.confirmDirtyNavigation(true, function () {
// If the user has said they want to navigate away, then mark all dirty pages with the cancelled
// property then do the navigating again. No pages will then prevent the navigation, unlike this
// first run.
_.each(dirtyPageModels, function (dirtyPage) {
dirtyPage.cancelled = true;
});
our.namespace.sammy.setLocation(new_location);
});
// Returns false in order to explicitly cancel the navigation. We don't need to return anything in any
// other case.
return false;
}
}
请记住,如果用户明确更改位置,此解决方案将不起作用,但这不是我们想要支持的用例。希望这能让您更接近自己的解决方案。
关于javascript - 使用 hashbang 防止用户在单页应用程序中离开路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949482/
这个问题在这里已经有了答案: 8 年前关闭。 Possible Duplicate: how to use multiple arguments with a shebang (i.e. #!)? 我
有谁能告诉我怎么做吗?这是我在此处用于动态加载内容的调用: jQuery(document).ready(function($) { function load(num) {
所以我一直在调查整个 SEO AJAX 恶意软件,并在这里设置了一个测试页面:My Site 我使用了来自 GitHub 的示例代码,如您所见,hashbang #! 工作正常,但请尝试访问任何将 h
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 10 个月前。 Impro
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
这个问题在这里已经有了答案: AngularJS routing without the hash '#' (10 个回答) angularjs 1.6.0 (latest now) routes n
除了让 Google 可以抓取 AJAX 内容之外,shebangs/hashbangs 是否还有其他用途?还是它? 最佳答案 在 Ajax 发明之前很久就已经存在 URL 中使用的散列。 它最初是作
我正在为我的单页应用程序使用 Sammy.js。我想创建类似于 SO 的功能(当您输入问题并尝试离开页面时,它会询问您是否确定)。 如果它不是单页应用,我会做类似的事情: $(window).bind
我有一个单页应用程序,可以打开多个选项卡,每个选项卡执行不同的操作。然而,如果用户点击刷新或返回,他们就会失去他们正在处理的所有内容。有没有办法(可能结合使用 hash-bangs 和 cookie)
我现在的位置:我已经阅读了 Google 关于 AJAX 抓取的文档,并且在这个网站和其他网站上进行了一些搜索,但我很困惑,因为它似乎所有问题都解决了同一个问题:AJAX crawing with h
除了谷歌提出的 hashbang 方法之外,谁能给我一个不使用 hijax(渐进式增强)方法的充分理由?据我所知,hijax 方法仍然是更好的方法: 它适用于非 javascript 浏览器 所有搜索
我正在创建一个 React 应用程序,并且有两个 URL,如下所示: /home /运动员 我想在这两个 URL 中的 hashbang 之后添加一个 q 参数。因此,这个 URL 基本上会在我所在的
最新版本的 Angular/Angular-Route 1.6.1 默认使用 hashbang 吗?就拿这段代码来说,我必须使用#!当链接到部分时,因为 #/或 #/partial2 不起作用。我认为
我读了here shebang (#!) 的想法太谷歌了 knows that an alternative conventional URL exists providing the same pa
为什么 Angular 有时在 URL 中使用散列,而其他时候使用散列?我已经开始从头开始编写两个 Angular 应用程序。两者都没有使用 HTML5 模式。两者都有相同的默认路由。但是,默认 UR
我正在构建一个网络应用程序,在后台使用 express.js,在前端使用 backbone.js。 我希望整个内部应用程序在客户端处理(主干请求数据并呈现它,服务器不直接返回任何 html)。 为了让
我希望将我的站点移动到完全异步文档加载中,但我不想使用请求处理的 #! 方法,因为 1) 我不想断开链接,和 2) 我想要一种更灵活的方式来处理站点获取的 URI。 我已经能够为我的网站构建一个轻型
我正在开展一个 Cordova 项目。我有一些带有监听器的链接,href 设置为“#”。 现在,在我的所有函数中,我提供事件并调用 PreventDefault()。 由于某种原因(在浏览器中访问应用
根据 Goolge's docs , 万能的谷歌蜘蛛会查看这个 url: www.example.com/ajax.html#!key=value 然后转到这个 url www.example.com
我开发了一个以非 ajax 页面为核心的网站,然后是一个带有 url 的分支 http://mywebsite.com/frameworks/#!ajaxpageaddress1 http://m
我是一名优秀的程序员,十分优秀!