javascript - Change Page Title After a Certain Time -
i trying dynamically change title of page after 2 seconds (2000 ms).
here code have tried:
<!doctype html> <html> <head> <title>before</title> </head> <body onload="updatetitle"> <script> var title = document.getelementsbytagname('title'); function updatetitle() { //here kind of delay (of 2 seconds). have tried settimeout(), setinterval() , delay() title.innerhtml = "after"; </script> dummy content body </body> </html>
how might modified after 2 seconds, page title changes?
for changing title, should rather using window.title
window.title = 'after';
in case, absolutely must use document.getelementsbytagname
, note returns array of elements, first of need make use of. i.e.
document.getelementsbytagname('title')[0].innerhtml = 'after';
of course, if page not have <title>
element, above result in error. first 1 (window.alert
) work in either case.
delay
for introducing delay, can use settimeout
(as you've done)
<script> function updatetitle() { settimeout(function() { window.title = 'after'; }, 2000); } </script>
full page working code:
notice block containing updatetitle()
has been moved . issue function updatetitle()
not yet defined when browser tried bind onload
(while parsing page) - defined afterwards inside body.
<!doctype html> <html> <head> <title>before</title> <script> function updatetitle() { settimeout(function() { document.getelementsbytagname('title')[0].innerhtml = 'after'; }, 2000); } </script> </head> <body onload="updatetitle()"> dummy content body </body> </html>
Comments
Post a Comment