0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ClojureScriptでページ内URL操作(pushState, popState)

Posted at

はじめに

pushState popStateとはReactを利用したサイトのように,ページ内遷移しか発生しなくとも,URLを書き換え履歴に追加し,ブラウザバックに対応することができる仕組みです.

javascriptやjQueryではやり方が多々紹介されているのですが,cljsで行う際に若干詰まったので一言備忘録を残しておきます(ほぼjsからの変換そのままです).

cljsでpushState, popState

(def data (atom {}))

...

;; pushState
(-> js/window .-history (.pushState #js {:data (pr-str data)} nil (str "/url"))))

;; popState
(set! (.-onpopstate js/window)
        #(when-not  (nil? (aget  % "state"))
           (when-let [d (cljs.reader/read-string (aget % "state" "data"))]
             (swap! data d))))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?