(질문수정) 게시글의 link의 target을 모두 바꾸려면? 정보
(질문수정) 게시글의 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);
댓글 전체

<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>

child node에 child node를 참조해서 하시면 될듯합니다. ^^;;
로빈아빠님께서 친절하게 답변해 주시네요 ^^

<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>
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

if (hrefs[i].childNodes[0]&&hrefs[i].childNodes[0].nodeName=="IMG")
이렇게 하심 되겠구만요..

<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>
<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>
생각대로 하는 소스~~