From eda26398e5ff737f5d734f80bfa0abcbbbbb8e81 Mon Sep 17 00:00:00 2001 From: Brandon Mills Date: Wed, 15 Feb 2017 00:02:17 -0500 Subject: [PATCH] [Docs] Use replaceState API when demo code changes (#710) In the current demo, every time the code in the left panel is changed, the new code is stored in the URL hash, which pushes a new history entry. This effectively "breaks" the back button. This change calls `history.replaceState` instead, so a single click of the back button will return the browser to whatever page was open before navigating to the demo. --- docs/index.html | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/index.html b/docs/index.html index 1bbb9d15..967e7c1b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -131,6 +131,20 @@ function getOptions() { return options; } +function replaceHash(hash) { + if ( + typeof URL === "function" && + typeof history === "object" && + typeof history.replaceState === "function" + ) { + var url = new URL(location); + url.hash = hash; + history.replaceState(null, null, url); + } else { + location.hash = hash; + } +} + function format() { var options = getOptions(); [docEditor, outputEditor].forEach(function(editor) { @@ -146,7 +160,7 @@ function format() { Object.assign({content: inputEditor.getValue(), options: options}) ) ); - location.hash = value; + replaceHash(value); var res; try { res = prettier.format(inputEditor.getValue(), options);