There is no way in only JS to due it, because cross site scripting is not allowed.
You could call a CGI script to be the source of the js, and have it output the script to write it.
On your page you might have
Then if this was perl, gettitle.cgi might be