在新窗口中打开外部链接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在新窗口中打开外部链接相关的知识,希望对你有一定的参考价值。

Script takes links that are not on your domain and opens them in a new window. This code allows for Xhtml strict validation and meets Accessibility criteria on opening new windows.
  1. function handleExternalLinks() { // function makes sure that external links open in new window
  2. var hostName = window.location.hostname;
  3. var links = document.getElementsByTagName("a");
  4. for(var i = 0; i < links.length; i++) {
  5. if(links[i].href.indexOf(hostName) == -1) {
  6. var curTitle = (links[i].getAttribute("title")) ? links[i].getAttribute("title") + " - ": "";
  7. links[i].setAttribute("target", "_blank");
  8. links[i].setAttribute("title", curTitle + "opens in new window");
  9. }
  10. }
  11. }
  12.  
  13. handleExternalLinks(); // Call the function

以上是关于在新窗口中打开外部链接的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 在新窗口中打开外部链接

在新窗口中打开外部链接

在新窗口中打开外部链接

在新窗口中打开外部链接

JavaScript jQuery在新窗口中打开外部链接

自动在新窗口中打开每个外部链接(html、css)