完好用户体验: 活用window.location与window.open解决页面跳转问题
原文日期: 2014年08月27日
翻译日期: 2014年08月31日
翻译人员: 铁锚
(译者注: 本文解决的是按 Ctrl键时使用JS打开新页面的问题)
在简化的HTML5规范中,同意在 A 标签内包括多个 DIV 和/或其它块级元素. 如今仅仅要用 <a> 标签包住块元素,就能搞定原来须要用JavaScript来监听并调用 window.location 实现页面跳转(redirect)功能.
但使用<a>标签的这样的包装形式也有不好使的情况 —— 比如,某个块元素(block)内另一些 <a> 标签, 这样的情况下我们仅仅想在点击parent中<a>以外的其它部分时才跳转到一个给定的地址。
当然,像以下这样用一个简单的listener 也能实现我们的需求:
someElement.addEventListener('click', function(e) {
// URL地址是什么都行,或者你也能够使用其它的代码来指定.
// 此处用的是该元素的 `data-src` DOM属性(attribute)
window.location = someElement.get('data-url');
});
…但这有时会有非常糟的用户体验, 当按住CTRL键(Mac是COMMAND键),再用鼠标点击时,它会在同一个(标签页)窗体内打开链接。
知道有这个问题,你肯定想到了该怎样去解决.我们改动一小点代码就能达成这个目的,赶快花点时间去修复你的listener吧:
someElement.addEventListener('click', function(e) {
// 获取URL
var url = someElement.get('data-url');
// 推断是否按下了CTRL键
if(e.metaKey || e.ctrlKey || e.button === 1) {
window.open(url);
} else {
window.location = url;
}
});
原文作者已经在 http://davidwalsh.name/ 站点上实现了这个功能,在使用window.location进行页面重定向时你也应该记得这一点。这是一个非常小的代码改进,但对可用性的提高却是非常重要的!
完好用户体验: 活用window.location与window.open解决页面跳转问题的更多相关文章
- window.location.hash(hash应用)---跳转到hash值制定的具体页面
location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location. ...
- window.location 与window.open区别
window.location 与window.open区别 1.window.location是window对象的属性,而window.open是window对象的方法 window.locat ...
- window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应
错误写法 , 主要是在 href="#"这里 <a href="#" id="send" onclick="return b ...
- window.location和window.open
window.location和window.open的区别 window.location = "http://www.baidu.com" 跳转后有后退功能 window.lo ...
- window.location和window.open的区别
window.location = "http://www.baidu.com" 跳转后有后退功能 window.location.replace("http://www ...
- window.location.href = window.location.href window.location.reload()
w 0-会议预订提交了预订日期,预订成功后默认显示仅显示当前日期的新页面若显示预定日的信息,则可以对预定日存入cookie: http://stackoverflow.com/questions/24 ...
- window.location与window.open()的区别
"top.location.href"是最外层的页面跳转"window.location.href"."location.href"是本页面 ...
- window.location和window.location.href和document.location的关系
1,首先来区分window.location和window.location.href. window.location.href是一个字符串. 而window.location是一个对象,包含属性有 ...
- js 中实现页面跳转的方法(window.location和window.open的区别)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
随机推荐
- HDOJ 1398 Square Coins
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ2771 Guardian of Decency
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5513 Accepted: 2319 Description Frank ...
- js81:Image对象,几张图像缓存完之后动画显示,form.elements[],document.images[]
原文发布时间为:2008-11-09 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串
题目链接 题意 给定字符串\(s\),可以在其中任意位置插入字符\(x\). 问能否得到一个回文串,若能,需插入多少个\(x\). 思路 首先统计出现次数为奇数的字符\(cnt\). \(cnt\ge ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- jdk、maven、tomcat环境变量配置
1.jdk 新建环境变量: JAVA_HOME:C:\Program Files\Java\jdk1.8.0_91 CLASSPATH:.;%JAVA_HOME%\lib;%JAVA_HOME%\li ...
- hdu 4991(树状数组+DP)
Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- LeetCode OJ-- Text Justification
https://oj.leetcode.com/problems/text-justification/ 细节题 class Solution { public: vector<string&g ...
- 51Nod 1019 逆序数(线段树)
题目链接:逆序数 模板题. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a) ...