![]() |
![]() ![]() |
![]() |
PeterR |
![]()
Post
#1
|
Group: Members Posts: 4 Joined: 15-March 23 Member No.: 28,855 ![]() |
My index.html script below has a problem.
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <script> function redirectFunc() { window.open("https://www.google.com"); } setTimeout("redirectFunc()", 8000); //window.close(); </script> </body> </html> Currently it loads google.com url after 8 seconds but my desire is to load google.com for a period of 8 seconds, then close the google browser tab. I don't see where my mistake is in above code!? Help resolving this would be greatly appreciated! |
Brian Chandler |
![]()
Post
#2
|
Jocular coder ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,489 Joined: 31-August 06 Member No.: 43 ![]() |
My index.html script below has a problem. <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <script> function redirectFunc() { window.open("https://www.google.com"); } setTimeout("redirectFunc()", 8000); //window.close(); </script> </body> </html> Currently it loads google.com url after 8 seconds but my desire is to load google.com for a period of 8 seconds, then close the google browser tab. I don't see where my mistake is in above code!? Help resolving this would be greatly appreciated! I'm only guessing, but perhaps setTimeout(x, 8000) means "Do x after 8 seconds". Since x is "open a google window" this indeed happens after 8 seconds. Then you have commented out "close the window". I think you meant to set the timer for "close the window", and then call your redirect function (not commented out). No obvious point in not directly writing the window.open() function though. And you should not be able to close the window without at least a confirmation from the user. |
abuislam |
![]()
Post
#3
|
Group: Members Posts: 7 Joined: 21-February 25 Member No.: 29,396 ![]() |
You can achieve this by opening the new tab and using setTimeout to close it after 8 seconds. Try this:
html Copy Edit <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <script> function redirectFunc() { let newTab = window.open("https://www.google.com"); setTimeout(() => { if (newTab) newTab.close(); }, 8000); } redirectFunc(); </script> </body> </html> This should open Google and close it after 8 seconds. Hope this helps! |
![]() ![]() |
![]() |
Lo-Fi Version | Time is now: 19th March 2025 - 12:47 AM |