vmars316 5 Posted November 8, 2020 Report Share Posted November 8, 2020 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 Quote Link to post Share on other sites
Ingolme 1,020 Posted November 8, 2020 Report Share Posted November 8, 2020 Use window.open() to open a URL in a new tab. 1 Quote Link to post Share on other sites
vmars316 5 Posted November 8, 2020 Author Report Share Posted November 8, 2020 (edited) 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 November 8, 2020 by vmars316 added info Quote Link to post Share on other sites
ishan_shah 2 Posted November 9, 2020 Report Share Posted November 9, 2020 You can use it like this: window.open('https://www.w3schools.com', '_blank'); 1 Quote Link to post Share on other sites
vmars316 5 Posted November 9, 2020 Author Report Share Posted November 9, 2020 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... Quote Link to post Share on other sites
dsonesuk 913 Posted November 10, 2020 Report Share Posted November 10, 2020 I think you talking about preventDefault() https://www.w3schools.com/jsref/event_preventdefault.asp Quote Link to post Share on other sites
vmars316 5 Posted November 12, 2020 Author Report Share Posted November 12, 2020 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> ....Navigation CANCELED. </h4> </body> </html> Thank you Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.