From a2aced4f36f8946069b4f21cd103562a1259031f Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Thu, 1 Feb 2018 14:02:35 -0500 Subject: [PATCH] Add more documentation and tests for options.currentUrlFullReload Closes #17 --- README.md | 26 ++++++++++++++++++++++++++ lib/proto/parse-options.js | 1 + tests/lib/proto/parse-options.js | 1 + 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 04bf3ed..54b2665 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,32 @@ Enables verbose mode. Useful to debug page layout differences. When set to true, clicking on a link that points to the current URL will trigger a full page reload. +The default is `false`, so clicking on such a link will do nothing. +If you want to add some custom behavior, add a click listener to the link, +and set `preventDefault` to true, to prevent Pjax from receiving the event. + +Here is some sample code: + +```js + var links = document.querySelectorAll(".js-Pjax"); + + for (var i = 0; i < links.length; i++) { + var el = links[i] + + el.addEventListener("click", function(e) { + if (el.href === window.location.href.split("#")[0]) { + e.preventDefault(); + console.log("Link to current page clicked"); + // Custom code goes here. + } + }) + } +``` + +(Note that if `cacheBust` is set to true, the code that checks if the href +is the same as the current page's URL will not work, due to the query string +appended to force a cache bust). + ##### `timeout` (Integer, default: `0`) The timeout in milliseconds for the XHR requests. Set to 0 to disable the timeout. diff --git a/lib/proto/parse-options.js b/lib/proto/parse-options.js index b7c1937..8118d50 100644 --- a/lib/proto/parse-options.js +++ b/lib/proto/parse-options.js @@ -24,6 +24,7 @@ module.exports = function(options) { options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust options.debug = options.debug || false options.timeout = options.timeout || 0 + options.currentUrlFullReload = (typeof options.currentUrlFullReload === "undefined") ? false : options.currentUrlFullReload // We can’t replace body.outerHTML or head.outerHTML. // It creates a bug where a new body or head are created in the DOM. diff --git a/tests/lib/proto/parse-options.js b/tests/lib/proto/parse-options.js index c36d96e..4665bcb 100644 --- a/tests/lib/proto/parse-options.js +++ b/tests/lib/proto/parse-options.js @@ -23,6 +23,7 @@ tape("test parse initalization options function", function(t) { t.equal(pjax.options.scrollRestoration, true) t.equal(pjax.options.cacheBust, true) t.equal(pjax.options.debug, false) + t.equal(pjax.options.currentUrlFullReload, false) t.end() })