forked from mariusGundersen/ajax-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.html
54 lines (53 loc) · 2.52 KB
/
dynamic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!doctype html>
<title>dynamic</title>
<button onclick="addGeneratedForms()">addGeneratedForms</button>
<script>
function addGeneratedForms(){
var div = document.createElement('div');
div.innerHTML = '<form class="login" method="post" action="login">\
<input type="hidden" name="redirectTo" value="/dynamic.html">\
<input type="text" name="username">\
<input type="password" name="password">\
<button type="submit">login</button> go back to this page\
</form>\
<form class="login" method="post" action="login">\
<input type="hidden" name="redirectTo" value="/success.html">\
<input type="text" name="username">\
<input type="password" name="password">\
<button type="submit">login</button> go to success page\
</form>\
<form class="login" method="post" action="login">\
<input type="hidden" name="redirectTo" value="/dynamic.html?success=true">\
<input type="text" name="username">\
<input type="password" name="password">\
<button type="submit">login</button> go back to this page with query param\
</form>\
<form class="login" method="post" action="login" data-push-state>\
<input type="text" name="username">\
<input type="password" name="password">\
<button type="submit">login</button> stay on this page but update the url with pushState\
</form>';
document.body.appendChild(div);
}
document.body.addEventListener('submit', function ajax(e){
e.preventDefault();
setTimeout(function(){
if(e.target.hasAttribute('data-push-state')){
// Only works in chrome when the original login form does not exist anymore after push (or ajax)
// So either remove (or hide) the form or try to change it's action url (? - didn't test)
// Also make sure all other forms (see above) point to a different action url
// otherwise they are considered as login form too (to test just rename the action attributes).
e.target.parentNode.removeChild(e.target);
// Or alternatively just hide the form: e.target.style.display = 'none';
history.replaceState({success:true}, 'title', "/success.html");
// This is working too!!! (uncomment the history.xxxState(..) line above before) (it ALSO works when the http response isn't a redirect but just a 200)
//var request = new XMLHttpRequest();
//request.open('POST', '/login', true);
//request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
//request.send("redirectTo=/dynamic.html");
}else{
e.target.submit();
}
}, 1);
}, false);
</script>