在新窗口中打开外部链接
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.
function handleExternalLinks() { // function makes sure that external links open in new window var hostName = window.location.hostname; var links = document.getElementsByTagName("a"); for(var i = 0; i < links.length; i++) { if(links[i].href.indexOf(hostName) == -1) { var curTitle = (links[i].getAttribute("title")) ? links[i].getAttribute("title") + " - ": ""; links[i].setAttribute("target", "_blank"); links[i].setAttribute("title", curTitle + "opens in new window"); } } } handleExternalLinks(); // Call the function
以上是关于在新窗口中打开外部链接的主要内容,如果未能解决你的问题,请参考以下文章