The following works for me:
<div id="divref">
<script>
window.onload = domainRef;
function domainRef() {
var ref = document.referrer.toString();
document.getElementById("test").value = ref;
}
</script>
</div>
<input type="hidden" id="test" value="">
The problem is that most likely the DOM has not been fully loaded when theonpageshow
event is triggered. A similar solution is to trigger the function using the onload
event attribute on the body element.
<body onload=domainRef()>
<div id="divref">
<script>
function domainRef() {
var ref = document.referrer.toString();
document.getElementById("test").value = ref;
}
</script>
</div>
<input type="hidden" id="test" value="">
</body>
For the latter solution I have created a jsFiddle where I have removed the hidden attribute to show the input field with the resulting value.