未加星标

Stop hijacking my middle click on links, in 10 lines

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
filter-altered-clicks

Filter alt-click, ctrl-click, shift-click, middle click, ...

Middle-clicking on a link should open it in a new tab. SPAs hijack normal links to load them via ajax, breaking all altered clicks ... unless they filter-altered-clicks :wink:

Altered clicks are:

ALT -click CTRL -click SHIFT -click CMD -click Any clicks that aren’t left-clicks Clicks that already received preventDefault() .Note Usage

filterAlteredClicks(listener) accepts a function and returns a function.

Simplest usage:

element.addEventListener('click', filterAlteredClicks(function (e) { console.log('Unaltered click!'); }));

jQuery usage:

$(element).on('click', filterAlteredClicks(function (e) { console.log('Unaltered click!'); }));

Ajax loading example, using jQuery for brevity, but it's not necessary:

$('a.ajax-link').on('click', filterAlteredClicks(function (e) { $('#content').load(this.href); e.preventDefault(); })); Install

Pick your favorite:

<script src="dist/filter-altered-clicks.browser.js"></script> npm install --save filter-altered-clicks var filterAlteredClicks = require('filter-altered-clicks'); import filterAlteredClicks from 'filter-altered-clicks'; API filterAlteredClicks(listener, [onlyPhysical])

Returns a listener function that is called by addEventListener or jQuery.on . Receives the Event as the first parameter.

listener

Type: function

The same listener function you would pass to addEventListener(type, listener) and .on(type, listener) .

onlyPhysical

Type: boolean , defaults to false

Once filtered, listener is normally not called if the event has already been defaultPrevented . Set this parameter to true to avoid this behavior.

Example:

el.addEventListener('click', filterAlteredClicks(function (e) { console.log('Unaltered click!'); console.log('I’m altering this click:'); e.preventDefault(); })); el.addEventListener('click', filterAlteredClicks(function (e) { // This will never be called because the previous one used .preventDefault })); el.addEventListener('click', filterAlteredClicks(function (e) { console.log('Unaltered click! But maybe .preventDefault was already called'); }, true)); //<-- notice the true as the second parameter of filterAlteredClicks

This is called onlyPhysical because it refers to "only physical alterations", which is altered by keyboard and not by code.

Dependencies

None!

Related on-off: Add and remove multiple events on multiple elements in <1KB License

MIT Federico Brigante

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: jQueryAjax
分页:12
转载请注明
本文标题:Stop hijacking my middle click on links, in 10 lines
本站链接:http://www.codesec.net/view/480985.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(24)