local-links 1.4.1

Determine cross-browser if an event or anchor element should be handled locally.

Homepage: https://github.com/lukekarrys/local-links

Platform: npm

Language: JavaScript

License: MIT

Keywords: IE, links, local

View on registry: https://www.npmjs.com/package/local-links

Direct download link: https://registry.npmjs.org/local-links/-/local-links-1.4.1.tgz

Install: npm install local-links@1.4.1


local-links

Determine cross-browser if an event or anchor element should be handled locally.

NPM Build Status Sauce Test Status

Install

npm install local-links --save

Why?

Browsers have quirks. Knowing if a link is local should be easy, since we just want to know if the hosts are the same. But this can be difficult because of the aforementioned browser quirks. A few of them:

  • IE9 will add :80 to the host of an anchor, but not the window
  • IE9 wont put a leading slash on the pathname of an anchor, but will on the window
  • Chrome 36 will report anchor.hash as '' if it has href="#"
  • More? Please report test cases!

Because of that and a few other things I was doing all the time, such as finding the closest anchor to an element based on an event object, I decided it would be a good module (that at least I would use all the time).

Usage

<a href='/page2' id="local">Local</a>
<a href='#hash' id="hash">Local</a>
<a href='http://google.com' id="google">Google</a>
var local = require('local-links');

// `pathname()` will return the pathname as a string
// if the link is local, otherwise it will return null
local.pathname(document.getElementById('local')) // '/page2'
local.pathname(document.getElementById('hash')) // null
local.pathname(document.getElementById('google')) // null

// `hash()` will return the hash as a string
// if the hash is to this page, otherwise it will return null
local.hash(document.getElementById('local')) // null
local.hash(document.getElementById('hash')) // '#hash'
local.hash(document.getElementById('google')) // null

API

Methods

getLocalPathname(Event or HTMLElement [, HTMLElement])

Returns the pathname if it is a non-hash local link, or null if it is not. Always includes the leading /.

Alias: pathname

getLocalHash(Event or HTMLElement [, HTMLElement])

Returns the hash if it is an in-page hash link, or null if it is not. Always includes the leading #.

Alias: hash

isActive(Event or HTMLElement [, String comparePath])

Returns true/false depending on if the anchor pathname is equal to the comparePath (which defaults to window.location.pathname). Calls pathname() internally.

Alias: active

isLocal(event, anchor, [, Boolean lookForHash])

Returns the pathname (or hash if lookForHash is true) for local links, or null if it is not. This is used by pathname() and hash() under the hood. The main difference here is that you need to specify the event and anchor yourself, and the anchor wont be looked up from event.target like it would from the other methods.

Supply either Event or HTMLElement

The above methods will accept an Event object, like the one you get from click event handlers, or any HTMLElement. You can also supply an Event object and a different HTMLElement as the second parameter and it will take precedence.

If only an Event object is supplied, the HTMLElement will be found from Event.target.

Nested HTML Elements

In the case that any HTMLElement your provide is not an anchor element, the module will look up parentNodes until an anchor is found.

Events

If an Event object is supplied, all methods will return null if any of the following are true altKey, ctrlKey, metaKey, shiftKey. This is because you almost always want to treat modified click events as external page clicks.

target="_blank"

If the anchor has target="_blank" it will return null for both the pathname() and hash() methods.

Hash links

Using the pathname method will return null for hash links that do not point to a different page. To get the hash for one of these links use the hash() method.

Tests

Run npm start and open http://localhost:3000 to run the tests in your browser.

It is also a good idea to run sudo npm run start-80 (requires admin) which will run the tests on http://localhost because there can be unexpected behavior when the host has no port in IE9 and IE10.

To run the tests in the cli, just run npm test.

License

MIT


Development Dependencies Requirements Latest Stable Latest Release Licenses
browserify ^13.1.0 14.1.0 14.1.0 MIT
electron-prebuilt ^1.3.2 1.4.13 1.4.13 MIT
git-validate ^2.1.4 2.2.2 2.2.2 MIT
jquery ^3.1.0 3.2.0 3.2.0 MIT
lodash.partial ^4.2.0 4.2.1 4.2.1 MIT
run-browser ^2.0.2 2.0.2 2.0.2 MIT
standard ^8.0.0-beta.3 9.0.2 10.0.0-beta.2 MIT
tape ^4.6.0 4.6.3 4.6.3 MIT
tape-run ^2.1.4 2.4.0 2.4.0 MIT
tap-spec ^4.1.1 4.1.1 4.1.1 MIT
zuul ^3.10.3 3.11.1 3.11.1 MIT
zuul-ngrok ^4.0.0 4.0.0 4.0.0 MIT
Explore the resolved development dependency tree for local-links 1.4.1

Releases

  • 1.4.1 - September 01, 2016 23:56
  • 1.4.0 - April 21, 2015 06:53
  • 1.3.0 - February 24, 2015 06:21
  • 1.2.0 - February 10, 2015 21:19
  • 1.1.0 - February 07, 2015 22:09
  • 1.0.10 - December 04, 2014 01:43
  • 1.0.9 - December 04, 2014 00:57
  • 1.0.8 - October 08, 2014 22:12
  • 1.0.7 - October 08, 2014 20:39
  • 1.0.6 - August 15, 2014 00:43
See all 16 releases

Project Statistics

SourceRank 14
Dependencies 0
Dependent projects 5
Dependent repositories 144
Total releases 16
Latest release
First release
Stars 32
Forks 3
Watchers 3
Contributors 2
Repo Size: 39.1 KB

Top Contributors See all

Luke Karrys john chesley

Something wrong with this page? Make a suggestion

Export .ABOUT file for this library

Last synced: 2016-10-24 03:03:17 UTC

Login to resync this project