vmars316 Posted November 8, 2020 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 Link to comment Share on other sites More sharing options...
Ingolme Posted November 8, 2020 Share Posted November 8, 2020 Use window.open() to open a URL in a new tab. 1 Link to comment Share on other sites More sharing options...
vmars316 Posted November 8, 2020 Author 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 Link to comment Share on other sites More sharing options...
ishan_shah Posted November 9, 2020 Share Posted November 9, 2020 You can use it like this: window.open('https://www.w3schools.com', '_blank'); 1 Link to comment Share on other sites More sharing options...
vmars316 Posted November 9, 2020 Author 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... Link to comment Share on other sites More sharing options...
dsonesuk Posted November 10, 2020 Share Posted November 10, 2020 I think you talking about preventDefault() https://www.w3schools.com/jsref/event_preventdefault.asp Link to comment Share on other sites More sharing options...
vmars316 Posted November 12, 2020 Author 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 Link to comment Share on other sites More sharing options...
lovelmark Posted June 15, 2021 Share Posted June 15, 2021 (edited) 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 June 16, 2021 by Ingolme Removed link Link to comment Share on other sites More sharing options...
_brym Posted September 21, 2021 Share Posted September 21, 2021 But you're then technically manipulating their browsing history. Privacy laws in some countries might not permit that. Link to comment Share on other sites More sharing options...
Dhruv Lathia Posted October 16, 2023 Share Posted October 16, 2023 Thanks @ishan_shah It worked on my code Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now