简体   繁体   中英

Close the current tab and open a new - works only in Chrome

This code is intended a open a new tab and close the current:

<a target="_blank" title="It'll be opened in the new tab." onclick="javascript:window.close();" href="123">My link</a>

It works in Chrome, but in Firefox and Safari it doesn't close close the current tab.

The issue here is your using onclick and href on your href try putting

 onclick="location.href='Place URL Here'" 

Keep in mind that google chrome is very lenient when it comes to coding you can have hundreds of warnings and errors and the possibility for everything to display properly firefox is very strict interpretation of W3C HTML5 standards so a fallback might be useful and safari does not comply with W3C standards apple just kinda does their own thing so coding for safari might be a little more challenging

OR Since you want the page to close and want another to open you could simply just put an onclick which will open in the current browser so basically like a redirect button

 //Hyperlink <a href="URL Here">Value Here</a> //Button <input type=button onclick="location.href='URL Here'" value="Value Here"> 

Ethier one of these codings will work perfectly for what your trying to accomplish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM