Jump to content

How to: window.location.href target="_blank" ?


vmars316

Recommended Posts

Tia;

Trying to redirect from one page to another page

and still leave 1st page open .

This works except that it closes 1st page:

 

    window.location.href = "https://www.w3schools.com ";  // works fine

This doesn't work at all:

    window.location.href = "https://www.w3schools.com target="_blank" ";  // doesn't work

Full code:

<!DOCTYPE html>
<html>
<head>
<script>
function CreateJsListener() {
   document.querySelector('a').onclick = Redirect();
  }
</script>
</head>

<body onload="CreateJsListener()">


<a href="https://www.google.com"> https://www.google.com </a>

<script>
var timesThru = 0 ;
  
function Redirect() {
    alert('Redirecting To:  https://www.w3schools.com instead'); 
if (timesThru > 0) {
    alert('Redirecting To:  https://www.w3schools.com instead'); 
    window.location.href = "https://www.w3schools.com target="_blank" ";  // doesn't work
    window.location.href = "https://www.w3schools.com ";  // works fine
   }
	timesThru = timesThru + 1 ;
   }
	
</script>

</body>
</html>

Tia

 

Link to comment
Share on other sites

Thanks Ingolme;

Yes , this works perfectly , the 'href' allows me to keep current window open:

<!DOCTYPE html>
<head>
<script>
function AddEventListner(){
document.querySelector('a').addEventListener('click', event => {
  OpenSecondPage()
});
}
</script>
<head>
</html>
<body onload="AddEventListner"()>
<a href="https://www.w3schools.com" target="_blank">Visit W3Schools</a>
<script>
function  OpenSecondPage() {
window.location.href = "https://www.duckduckgo.com"; // default page
};
</script>
</body>
</html>

 

Thanks Again 

Btw: I miss your foxy Avatar .  I would like to have a copy of it possible ?

 

Edited by vmars316
added info
Link to comment
Share on other sites

Thanks ishan_shah ;

I had tried this: window.open('https://www.duckduckgo.com' ; 'target= "_blank" ') 

But couldn't find the proper format .

Turns out  my code above doesn't work , but this works except for one issue:

<!DOCTYPE html>
<html>
<head>
<script>
function AddEventListner(){
var timesThru = 0;
document.querySelector('a').addEventListener('click', event => {
  if  (timesThru > 0 ) {
  alert('Opening SecondPage() , timesThru = ' + timesThru )
    window.open('https://www.duckduckgo.com', '_blank'); // keeps page one active
    // window.location.href = "https://www.duckduckgo.com"; // closes page one , opens page two
}
  if  (timesThru < 1 ) { alert('timesThru =  ' + timesThru) }
timesThru = timesThru + 1 ;
}
)};
</script>
</head>
<body onload="AddEventListner()">
<a href="https://www.w3schools.com" target="_blank" >Visit W3Schools</a>
</body>
</html>

1st time thru , it opens the clicked Link . 

How can I prevent the '1st time thru Link from opening ? 

Thanks for your Help...

Link to comment
Share on other sites

Ah..... Finally;

<!DOCTYPE html>
<html>
<head>
<title> AddEventListener-For-A-Tag-Stop-Navigation </title>
<script>
function CreateJsListener() {
  document.querySelector('a').addEventListener('click', function(event) {Redirect(event) } )  } ;
</script>
</head>

<body onload="CreateJsListener()">


<a href="https://www.google.com"> https://www.google.com </a>

<script>
var timesThru = 0 ;
  
function Redirect(event) {
    event.preventDefault() ;
	window.open("NOT-A-SafeSite.html");
	timesThru = timesThru + 1 ;
   }
	
</script>

</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<title> NOT-A-SafeSite.html </title> 
</head>

<body>
<h4> The Link you clicked on is <span style="color: red">UNSAFE !</span> &nbsp; ....Navigation CANCELED. </h4>
</body>
</html>

Thank you  :)

Link to comment
Share on other sites

  • 7 months later...

You can redirect a web page via JavaScript using a number of methods. window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use location.href

If you want to simulate an HTTP redirect, use location.replace

JavaScript redirect example:

// similar behavior as an HTTP redirect

window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link

window.location.href = "http://stackoverflow.com";

 

Edited by Ingolme
Removed link
Link to comment
Share on other sites

  • 3 months later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...