테스트 사이트 - 개발 중인 베타 버전입니다

필요에 따라 자스/css 불러왔다 내보내기

· 13년 전 · 1540 · 3
주말에 제 홈피를 리뉴얼 중 (각 div 로 위아래 부드럽게 이동 하는 한장짜리 로 짜봤는데) 문제가 좀 생겼었습니다.

모든 jQuery 는 제가 직접 짠거라서 간단한데 반응형 jQuery 슬라이더를 넣으니 IE 에서 속도문제가 좀 생기는 것 같더라구요.

그래서 페이지가 아래로 이동하는 경우에는 슬라이더 관련 js 와 css 를 제거하고, 반대로 페이지가 상단위로 와 있는 경우는 블로그 페이지 관련 js 와 css 가 자동제거 되는 방식을 구현하려고 했는데....

힘들게 하나 찾은 자스 소스가....

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"

푸하하하

이걸 넣으면 페이지가 오히려 더 무거워지는 웃기는 상황발생.

조금더 살펴보니 jQuery 에 .remove !!!!!


$(  ).remove();  //이거 한줄이면 끝!!!!!

흡사한 방법으로 이것도 되네요

$.globalEval(js);


아, jQuery 의 위대함이란!!!! ㅋㅋㅋㅋ

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 3개

13년 전
저도 분석해가면서 제이쿼리를 짜내려 가고 싶은데 부럽습니다..
3개월된 퍼블리셔에겐 아직 쫒기에 먼 님이네요 ㅎㅎ;;

잘보구 갑니다~~
jquery 는 신세계죠.ㅎㅎ
Jquery 참 간결하죠 ㅋ

게시글 목록

번호 제목
10331
19987
17582
10327
10326
17579
17575
10318
10311
10300
19985
10299
10290
19981
10288
10286
10285
10273
19977
19968
19963
19959
17801
10264
10261