An open API service indexing awesome lists of open source software.

https://github.com/boneskull/jquery.preempt

Takes legacy inline JS (i.e. "onclick" and "href='javascript:...'") and creates event handler(s) to be run around the inlined code.
https://github.com/boneskull/jquery.preempt

Last synced: 8 months ago
JSON representation

Takes legacy inline JS (i.e. "onclick" and "href='javascript:...'") and creates event handler(s) to be run around the inlined code.

Awesome Lists containing this project

README

          

# jquery.preempt

[![Build Status](https://travis-ci.org/boneskull/jquery.preempt.png?branch=master)](https://travis-ci.org/boneskull/jquery.preempt)
[![Coverage Status](https://coveralls.io/repos/boneskull/jquery.preempt/badge.png?branch=)](https://coveralls.io/r/boneskull/jquery.preempt?branch=master)
[![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

[jQuery](http://jquery.com) plugin to take legacy inline JS (i.e. `onclick`
and `href="javascript:..."`) and create event handlers to be run before or
after the inlined code (or both!).

> #### (Why would I need this?)
> My use case was that I was writing a [GreaseMonkey](https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/)-like [script](https://github.com/boneskull/fb-bugmonkey-markdown) against a page with inline JS and my
jQuery events were not behaving. Works for me; your mileage may vary.

## Installation

Use the [jsDelivr CDN](http://www.jsdelivr.com/#!jquery.preempt).

## Usage

```javascript

// given do something
// or do something else

// Basic usage:
$('button').preempt({
attr: 'onclick',
event: 'click',
}, function doSomethingBeforeSomething() {
// do something else
});

// Restoring the inline JS:
$('button').preempt({
attr: 'onclick',
event: 'click',
restore: true
});

// Fancy usage:
$('button').preempt({
attr: 'onclick',
event: 'click',
before: function executedBeforeInlineJS(event, data) {
// stuff; return false to halt propagation to inline JS
},
after: function exectedAfterInlineJS(event, data) {
// things; return false to prevent the default action and stop propagation
},
// will execute the after() function even if the inlined JS returned false.
forcePropagation: true,
data: {
before: 'some data to be passed to the before() function',
after: 'some data to be passed to the after() function',
}
});
```

## Author
[Christopher Hiller](http://boneskull.github.io)