Create Welcome UserName

The code originated from W3cSchools and I stared to pick at it for my needs.  Everything works great but the "document.getElementById("title_script").innerHTML="Welcome "+username+" to my sandbox";" work great.  That script won't work on the first load but works on every load after that.  Ideas?