Hello, I hope someone can help me. I have taken the code “ESP32 Web Updater Over The Air” and it works very well.
But I try that once the username and password are entered in /loginIndex, the next page (/serverIndex) opens in the same tab and in the same browser window. The relevant part of the code is:
String loginIndex =
"<form name=loginForm>"
"<h1>ESP32 Login</h1>"
"<input name=userid placeholder='User ID'> "
"<input name=pwd placeholder=Password type=Password> "
"<input type=submit onclick=check(this.form) class=btn value=Login></form>"
"<script>"
"function check(form) {"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{window.open('/serverIndex')}"
"else"
"{alert('Error Password or Username')}"
"}"
"</script>" + style;
I have changed the command
"{window.open('/serverIndex')}"
for the following variants without success:
1
"{window.open('/serverIndex', '_ self')}"
2
"{location.href ='/serverIndex'}"
3
"{window.location.replace('/serverIndex'}"
4
"{window.location.href = '/serverIndex'}"
Could someone tell me which command I should use for /serverIndex to open in the same tab and window of /loginIndex ?
Thank you in advance
Hello, I’m not sure if I understood your question…
Do you want to open it in the same tab and same window? But doesn’t it do that already?
I’m not sure what you mean.
Thanks for your patience!
Hi Rui, thanks for asking. Yes, I need to open the /serverIndex page in the same tab, same window that the /loginIndex page. Currently the statement
"{window.open('/serverIndex')}"
opens /serverIndex in a new tab.
I’ve been trying to do this for days without success. Hope you’ll help me. Thanks in advance.
Thanks for clarifying Paul, this should solve your problem (adding ‘_self’ as the second parameter):
"{window.open('/serverIndex', '_self')}"
Does that work for you?
Hi Rui, thanks for your response. As you can check, in my first message I listed some statements I already tried without success. The one you suggested me is on of them.
I already tried with:
"{window.open('/serverIndex', '_ self')}"
"{location.href ='/serverIndex'}"
"{window.location.replace('/serverIndex'}"
"{window.location.href = '/serverIndex'}"
No one worked for me.
Which web browser are you using? One of those examples should work, because it’s the official API to open in the same tab… I’m not sure what’s going on with your exact setup…
Hi Rui, I already tested the code in Firefox, Edge and Chrome with the same results.
Even more, I took your code example from your “ESP32 Over-the-air (OTA) Programming – Web Updater Arduino IDE” and tried replacing the
"window.open('/serverIndex')"
with the above listed four statements:
"{window.open('/serverIndex', '_ self')}"
"{location.href ='/serverIndex'}"
"{window.location.replace('/serverIndex'}"
"{window.location.href = '/serverIndex'}"
The first one opens the new page in a new tab, with the other three nothing happens.
Paul
Hello Paul,
I only had time to properly test and give you the solution to this problem.
Here’s how to load the /serverIndex page in the same tab. You need to make two changes to the const char* loginIndex.
First change, in the very first line, add this attribute (action=’javascript:void(0);’). It should look as follows:
"<form name='loginForm' action='javascript:void(0);'>"
Then, you need to use this method to open the page in the same tab:
"{window.location.href = '/serverIndex'}"
In total, this is the full loginIndex variable:
const char* loginIndex = "<form name='loginForm' action='javascript:void(0);'>" "<table width='20%' bgcolor='A09F9F' align='center'>" "<tr>" "<td colspan=2>" "<center><font size=4><b>ESP32 Login Page</b></font></center>" "<br>" "</td>" "<br>" "<br>" "</tr>" "<td>Username:</td>" "<td><input type='text' size=25 name='userid'><br></td>" "</tr>" "<br>" "<br>" "<tr>" "<td>Password:</td>" "<td><input type='Password' size=25 name='pwd'><br></td>" "<br>" "<br>" "</tr>" "<tr>" "<td><input type='submit' onclick='check(this.form)' value='Login'></td>" "</tr>" "</table>" "</form>" "<script>" "function check(form)" "{" "if(form.userid.value=='admin' && form.pwd.value=='admin')" "{" "window.location.href ='/serverIndex';" "}" "else" "{" " alert('Error Password or Username')/*displays error message*/" "}" "}" "</script>";
Does that work for you?
Happy to hear that it worked, it also took me a bit to figure out why that happened. Regards,
Rui