Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Window.open fails

I have this HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script>
		function ls250_window() {
			window.open("show_summ_ls250.php");
			window.location = "my_projects.php";
		}	
	</script>
  </head>
  <body onLoad="ls250_window();">
  </body>
</html>

Open in new window


My intention is to open the window as indicated, then make the active window show my_projects.php. That part works.

The window.open does not. Is that because an absolute url is required? Like http://www.xyz.com/ (etc).

Or maybe a timer to make sure the window.open completes BEFORE the redirect?

Thanks
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

I believe it needs a full url
Avatar of Brian Tao
I don't think it's relevant. Either related path or absolute path will work in window.open.
The issue you're facing is that the window.location runs successfully before the window.open because javascript runs in an async manner.
What you can do is as you mentioned, adding a timer to the window.location call, or change the code to use something similar to a callback function as below:
function ls250_window() {
  var win=window.open("show_summ_ls250.php");
  win.onload = function(){window.location = "my_projects.php"};
}	

Open in new window

You don't need to use the full path with protocol and domain, just the path to the page
What is the full path ?

Use : window.location.assign("/
https://developer.mozilla.org/en-US/docs/Web/API/Window/location

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script>
		function ls250_window() {
			window.open("show_summ_ls250.php");
			window.location.assign("/my_projects.php");
		}	
	</script>
  </head>
  <body onLoad="ls250_window();">
  </body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Richard Korts
Richard Korts
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial