Jump to content

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


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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...