아

(질문수정) 게시글의 link의 target을 모두 바꾸려면?

아래의 코드로 모든 지정된 target link를 바꿀 수 있습니다.

아래코드에 추가로, target link의 child node에 img가 있는 경우
img의 onclick을 싹~ 비워버리는 것을 넣고 싶어요.

외부의 link가 있는 문서는 펌해서 넣으면,
이미지도 뜨구 link도 뜨는 문제가 있어서 그걸 해결하려는 겁니다.

수정해 주신분께 위의 5만 포인트도 드립니다.
---
//=======================================================
// 링크 자동 타겟 설정
// http://phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=52466
//=======================================================
function link_auto_target_inarea(this_s,target,type){
//this_s안의 a를 체크해서 타겟을 바꾼다.
//type가 true이면 target이 있어도 강제로 바꾼다. false면 없을 경우만 바꾼다
 //alert(this_s.childNodes.length);
 if(!target){target='_blank';}
 if(!type){type=false;}
 if(!this_s){return;}
 if(this_s.nodeType!=1){return;}
  //alert(this_s.nodeName);
 for(var i=0,m=this_s.childNodes.length;i<m;i++){
  var ta = this_s.childNodes[i];
  if(ta.nodeName=='A'){
  if(ta.href){
    if(ta.href.toLowerCase().indexOf('javascript')==0){
    continue;
    }else if((!ta.target || type) ){
    ta.target = target;
    }
  }
  }
  if(ta.childNodes.length>0){
  link_auto_target_inarea(ta,target,type);
  }
 }
 return;
}

link_auto_target_inarea(document.getElementById('writeContents'),'_blank',false);

|

댓글 9개

<html>

<head>
<script type="text/javascript">
function myTarget(){
document.getElementById('myAnchor').target="_blank"
}
</script>
</head>

<body>
<a id="myAnchor" href="http://www.java2s.com">Visit Java2s</a>
<form>
<input type="button" onclick="myTarget()" value="Make the link open in a new window!">
</form>
<p>Try the link before and after you have pressed the button!</p>
</body>

</html>
id 지정없이 element type만으로 ...
이걸 응용해서..

child node에 child node를 참조해서 하시면 될듯합니다. ^^;;

로빈아빠님께서 친절하게 답변해 주시네요 ^^
<a href=http://freeimage.kr >test not blank</a><BR>
<a href=http://freeimage.kr ><img src=http://www.google.co.kr/images/nav_logo7.png ></a><BR>
<a href=http://freeimage.kr ><img src=http://www.google.co.kr/images/nav_logo7.png ></a><BR>
<script>
var imgs,i;
hrefs=document.getElementsByTagName('a');
for(i in hrefs)
{
if (hrefs[i].href) {
if (hrefs[i].childNodes[0].nodeName=="IMG")
hrefs[i].target='_blank';
}
}

</script>
Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Timestamp: Sat, 19 Dec 2009 19:35:29 UTC


Message: 'hrefs[...].childNodes.0.nodeName' is null or not an object
Line: 847
Char: 3
Code: 0
아 그부분은 <a href=http://freeimage.kr ></a>처럼 내용이 없는것일껍니다.
if (hrefs[i].childNodes[0]&&hrefs[i].childNodes[0].nodeName=="IMG")
이렇게 하심 되겠구만요..
<span id='writeContents'>
<a href=http://freeimage.kr ></a><BR>
<a href=http://freeimage.kr target='_new'><img src=http://www.google.co.kr/images/nav_logo7.png onclick=alert(1)></a><BR>
<a href=http://freeimage.kr target='_new'><img src=http://www.google.co.kr/images/nav_logo7.png ></a><BR>
</span>

<script>
function link_auto_target_inarea(this_s,target,type){
//this_s안의 a를 체크해서 타겟을 바꾼다.
//type가 true이면 target이 있어도 강제로 바꾼다. false면 없을 경우만 바꾼다
//alert(this_s.childNodes.length);
if(!target){target='_blank';}
if(!type){type=false;}
if(!this_s){return;}
if(this_s.nodeType!=1){return;}
//alert(this_s.nodeName);
for(var i=0,m=this_s.childNodes.length;i<m;i++){
var ta = this_s.childNodes[i];
if(ta.nodeName=='A'){
if(ta.href){


if(ta.href.toLowerCase().indexOf('javascript')==0){
continue;
}

if (ta.childNodes[0]&&ta.childNodes[0].nodeName=="IMG") {
//onclick, target 다 무시함..
ta.childNodes[0].onclick='';
ta.target='_self';
}
}

}
}
if(ta.childNodes.length>0){
link_auto_target_inarea(ta,target,type);
}
}

link_auto_target_inarea(document.getElementById('writeContents'),'_blank',false);

</script>
<span id="writeContents">
<a href="http://sir.co.kr"><img src="" onclick="window.open('', '', '')" /></a>
</span>


<script type="text/javascript">
function link_auto_target_inarea(Container)
{
var A_tag = Container.getElementsByTagName("A");

for (var i=0; i<A_tag.length; i++)
{
IMG_tag = A_tag[i].getElementsByTagName("IMG");

for (var k=0; k<IMG_tag.length; k++)
{
IMG_tag[k].onclick = "";
}
}

return;
}

link_auto_target_inarea(document.getElementById('writeContents'));
</script>

생각대로 하는 소스~~
질문이 부적절하다고 생각되어 다시 10만 포인트 걸구 질문 합니다.
댓글을 작성하시려면 로그인이 필요합니다.

그누보드4 관련 질문은 QA 로 이전됩니다. QA 그누보드4 바로가기 기존 게시물은 열람만 가능합니다.

+
제목 글쓴이 날짜 조회
09-12-20 조회 1,624
09-12-20 조회 4,598
09-12-20 조회 1,045
09-12-20 조회 1,908
09-12-20 조회 1,570
09-12-20 조회 1,019
09-12-20 조회 1,595
09-12-20 조회 1,515
09-12-20 조회 1,779
09-12-20 조회 1,278
09-12-20 조회 1,222
09-12-20 조회 1,614
09-12-20 조회 1,316
09-12-20 조회 1,618
09-12-20 조회 2,246
09-12-19 조회 1,229
09-12-19 조회 2,069
09-12-19 조회 1,615
09-12-19 조회 981
09-12-19 조회 1,142