Created
February 13, 2014 16:26
-
-
Save furugomu/8978433 to your computer and use it in GitHub Desktop.
デッキの並び換えでページ遷移しない
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # デッキの並び換えでページ遷移しない | |
| return if location.href.indexOf('/deck') < 0 | |
| # ターボリンクのようなことをする | |
| replaceBody = (html) -> | |
| parser = new DOMParser() | |
| doc = parser.parseFromString(html, 'text/html') | |
| body = document.importNode(doc.body) | |
| document.documentElement.replaceChild(body, document.body) | |
| document.addEventListener 'submit', (e) -> | |
| form = e.target | |
| return if form.action.indexOf('/execup/') < 0 # 「上に移動」のフォーム | |
| e.preventDefault() | |
| req = new XMLHttpRequest() | |
| req.open('POST', form.action) | |
| req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') | |
| req.addEventListener 'load', -> | |
| console.log(req.status, req.statusText) | |
| replaceBody(req.responseText) | |
| req.addEventListener 'error', -> | |
| alert('error') | |
| console.log('error', req) | |
| params = [] | |
| for input in form.elements | |
| continue unless input.name | |
| params.push(encodeURIComponent(input.name)+'='+ | |
| encodeURIComponent(input.value)) | |
| req.send(params.join('&')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment