Home  >  Article  >  Web Front-end  >  Javascript controls how to open a page link in a new window_javascript skills

Javascript controls how to open a page link in a new window_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:25:261243browse

The first method is to use jquery to implement it. The principle is to first find the external link in the site, and then add the target=_blank" attribute to the link. The code is as follows:
jquery opens in a new window

Copy code The code is as follows:

$("a[href*='http://'] :not([href*='" location.hostname "']),[href*='https://']:not([href*='" location.hostname "'])")
. addClass("external")
.attr("target","_blank");

js opens in a new window
Copy the code The code is as follows:

var aTag = document.getElementsByTagName('a');
for(i in aTag){
aTag[ i].target='_blank';
}

html method
Add
to the latest page of the page. Copy the code as followsYou can open all pages in a new window
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn