• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

HTML – Open URL in same window and in same tab

Q&A Forum › Category: ESP32 › HTML – Open URL in same window and in same tab
0 Vote Up Vote Down
Paul asked 6 years ago

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

10 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

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!

0 Vote Up Vote Down
Paul answered 6 years ago

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.

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Thanks for clarifying Paul, this should solve your problem (adding ‘_self’ as the second parameter):

"{window.open('/serverIndex', '_self')}"

Does that work for you?

0 Vote Up Vote Down
Paul answered 6 years ago

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.

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

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…

0 Vote Up Vote Down
Paul answered 6 years ago

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

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

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?

0 Vote Up Vote Down
Paul answered 6 years ago

Thanks a lot Rui, it works.  I’ve been trying this for weeks.

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Happy to hear that it worked, it also took me a bit to figure out why that happened. Regards,
Rui

0 Vote Up Vote Down
Paul answered 6 years ago

Thank you very much Rui, you did a great job. Previously I had placed this query in several sites and none of them could help me.
Paul

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.