A client who has multiple contractors needs to login to my system and then open new tabs by linking to secured pages within my system. Authenication is done via ajax so a call to a validate url and posting your username and password will give you a cookie + valid session.
I was asked for an example of how to automate this login and then be able to open a link to a secure page. I didn't have alot of time and come up with this.
<html>
<body>
<form id="login" action="http://domain.com/validate" method="post" target="iframe" style="display:none;">
<input type="text" name="username" value="test">
<input type="text" name="password" value="admin">
</form>
<iframe name="iframe" style="display:none;"></iframe>
<script type="text/javascript">
window.onload = function() {
document.getElementById("login").submit();
};
</script>
<!-- Launch a new tab to view post -->
<a href="http://domain.com/post/id/1234" target="_blank">1234</a><br>
<a href="http://domain.com/post/id/2345" target="_blank">2345</a>
</body>
</html>
The code works fine, across all browser's I'm interested in as we handle IE < 9 with googles chrome frame.
Is there a better way to handle the form submission process? Currently the responce from the forms submit action is sent to the iframe. I did this to stop the page reloading. Can this be handled with javascript instead of an iframe?