0

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?

shapeshifter
  • 2,967
  • 2
  • 25
  • 39
  • I think what you'd be looking for is AJAX? It allows you to submit requests to the server without reloading the page. Have you looked into that? – Ian Oct 31 '12 at 01:17
  • I had tried a simple $.post call with jquery first, however you run into cross-site ajax issues where most modern browsers block it. Though having just looked into it again I have found an interesting SO answer to that problem http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery/11736771#11736771 – shapeshifter Oct 31 '12 at 01:37

0 Answers0