links 객체는 문서에 존재하는 <a href=""> 형식의 태그를 배열에 담습니다.

document.links.속성

메서드 설 명
links 문서에 존재하는 모든 <a>링크를 배열에 담습니다.
  • href: 링크에 연결된 URL
  • host: 링크에 연결된 호스트이름과 포트번호
  • hostname: 링크에 연결된 호스트이름
  • pathname: 도메인을 제외한 실제 파일 절대경로
  • port: 링크에 연결된 포트 번호
  • protocol: 링크에 연결된 프로토콜
      http: 또는 https: 인 형식
  • hash: 링크의 URL에 붙어있는 해시값
      Ex.) http://example.com/?q=w#hash
  • search: 링크의 URL 끝에 붙어 있는 쿼리
      Ex.) http://example.com/?q=w
  • target: target 속성의 값
link 새로운 링크를 만듭니다.

<a href="php">샵(#)</a><br/>
<a href="#" target="_blank">naver</a><br/>
<a href="http://habonyphp.com:2082">하보니 PHP</a><br/>

<script>
var link = document.links;

for(var i = 0; i < link.length; i++) {
  document.write( link[i].href + " [" + link[i].target +"]<br/>");
}
/*
결과
  http://localhost:8080/php []
  http://localhost:8080/# [_blank]
  http://habonyphp.com:2082/ []
*/
</script>

이제 새로운 링크를 만들어 보겠습니다.

<script>
var str = "하보니 php";

document.write( str.link("http://habonyphp.com") );
// <a href="http://habonyphp.com">하보니 php</a>
</script>

0 댓글