My script looks like this:
<script>
var queryString = window.location.search;
var pos = queryString.indexOf("?refer=");
var referEmail = queryString.substring(pos+7);
document.getElementById("referral-code").value = referEmail;
</script>
And the HTML codes look like this:
<form action="/" method="POST">
<label for="referral-code">Referrer's code (hidden): </label>
<input type="text" name="referral_code" id="referral-code"/>
<br/>
<label for="me-email">My Email: </label>
<input type="text" name="self_email" id="me-email"/>
<br/>
<input type="submit" value="Step Inside" />
</form>
I think it may be cleaner to put the <script>
tag in <head>
. However, I am not sure whether the script will wait to be executed until the DOM in <body>
is processed. If not, will document.getElementById("referral-code")
returns undefined
?
Does anyone have ideas about whether a script in <head>
will wait to be executed until the whole HTML is loaded? And should I put the DOM manipulating script in <head>
or before the closing </body>
?