diff --git a/templates/default/httpredirect-debug.php b/templates/default/httpredirect-debug.php
index 7b921779c8206d22b27cf45a79c5ae84d6100b64..78cb0e66101ad8e8c8e2c602405f34779b58a02a 100644
--- a/templates/default/httpredirect-debug.php
+++ b/templates/default/httpredirect-debug.php
@@ -1,5 +1,6 @@
 <?php 
 	$this->data['icon'] = 'debug.png';
+	$this->data['autofocus'] = 'sendlink';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
 
@@ -14,9 +15,6 @@
 		<pre style="overflow: scroll; border: 1px solid #eee"><?php echo $this->data['message']; ?></pre>
 		
 		<p>[ <a id="sendlink" href="<?php echo htmlentities($this->data['url']); ?>">send SAML message</a> ]</p>
-		<script type="text/javascript">
-			document.getElementById('sendlink').focus();
-		</script>
 
 		<h2>Debug mode</h2>
 		
diff --git a/templates/default/includes/header.php b/templates/default/includes/header.php
index a8403227b0ff36732d95d0e2477cbbc37f39443f..f11fdd583de1841adc36adf3bb61939ad951a0f3 100644
--- a/templates/default/includes/header.php
+++ b/templates/default/includes/header.php
@@ -12,8 +12,18 @@ if(array_key_exists('header', $this->data)) {
 
 	<link rel="stylesheet" type="text/css" href="/<?php echo $this->data['baseurlpath']; ?>resources/default.css" />
 	<link rel="icon" type="image/icon" href="/<?php echo $this->data['baseurlpath']; ?>resources/icons/favicon.ico" />
+	<script type="text/javascript" src="/<?php echo $this->data['baseurlpath']; ?>resources/script.js" />
 </head>
-<body>
+<?php
+$onLoad = '';
+if(array_key_exists('autofocus', $this->data)) {
+	$onLoad .= 'SimpleSAML_focus(\'' . $this->data['autofocus'] . '\');';
+}
+if($onLoad !== '') {
+	$onLoad = ' onload="' . $onLoad . '"';
+}
+?>
+<body<?php echo $onLoad; ?>>
 
 <div id="wrap">
 	
diff --git a/templates/default/login.php b/templates/default/login.php
index 6dc9a8c3881d139fdcea5a1c562a7896ab6f2834..4b6aade06315cd3a7fd7d88b775c72365d21c90b 100644
--- a/templates/default/login.php
+++ b/templates/default/login.php
@@ -1,13 +1,8 @@
 <?php 
 	if (!array_key_exists('icon', $this->data)) $this->data['icon'] = 'lock.png';
+	$this->data['autofocus'] = 'username';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
-	<script type="text/javascript">
-		function focusit() {
-			document.getElementById('username').focus();
-		}
-		window.onload = focusit;
-	</script>
 <div id="content">
 
 	<?php if (isset($this->data['error'])) { ?>
diff --git a/templates/default/post-debug.php b/templates/default/post-debug.php
index 79d4cac7275b8c54958b85d4c76ab93528ac6d3e..ad37b618550b8796927170e391f8f83f0c8dad1d 100644
--- a/templates/default/post-debug.php
+++ b/templates/default/post-debug.php
@@ -1,5 +1,6 @@
 <?php 
 	$this->data['icon'] = 'debug.png';
+	$this->data['autofocus'] = 'sendbutton';
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
 
@@ -19,10 +20,6 @@
 			<input type="submit" value="Submit the response to the service" id="sendbutton" />
 		</form>
 
-		<script type="text/javascript">
-			document.getElementById('sendbutton').focus();
-		</script>
-
 		<h2>Debug mode</h2>
 		
 		<p>As you are in debug mode you are lucky to see the content of the response you are sending:</p>
diff --git a/www/resources/script.js b/www/resources/script.js
new file mode 100644
index 0000000000000000000000000000000000000000..1d1fa03b4a29732aedf3280fbfbbaed46e2c1ab8
--- /dev/null
+++ b/www/resources/script.js
@@ -0,0 +1,11 @@
+/**
+ * Set focus to the element with the given id.
+ *
+ * @param id  The id of the element which should receive focus.
+ */
+function SimpleSAML_focus(id) {
+  element = document.getElementById(id);
+  if(element != null) {
+    element.focus();
+  }
+}