You could include a hidden element within your page and set the value you need within the hidden value, then when you need to reuse it, just reference the hidden field.
Working Example (Uses a textbox as opposed to a hidden field for viewing purposes)
HTML:
<input type="hidden" id="nameValue" />
Javascript:
<script type="text/javascript">
window.alert("Documento ha sido adjuntado!")
var name = prompt("Referencia del Documento: ", "");
//Sets the hidden value with "name"
document.getElementById('nameValue').value= name;
window.close();
</script>
As per your specific question:
<script type="text/javascript">
window.alert("Documento ha sido adjuntado!")
var name = prompt("Referencia del Documento: ", "");
window.close();
UpdateDatabase(name);
function UpdateDatabase(yourString)
{
//Servlet call to Update Database with "yourString"
}
</script>
<body>tag, or elsewhere into an element/node within thebodyelement?