M

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

주말에 제 홈피를 리뉴얼 중 (각 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개

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

잘보구 갑니다~~
jquery 는 신세계죠.ㅎㅎ
Jquery 참 간결하죠 ㅋ
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 1,830
13년 전 조회 1,685
13년 전 조회 7,192
13년 전 조회 1,968
13년 전 조회 1,496
13년 전 조회 3,855
13년 전 조회 2,191
13년 전 조회 4,596
13년 전 조회 2,368
13년 전 조회 2,045
13년 전 조회 1,586
13년 전 조회 1,578
13년 전 조회 1,827
13년 전 조회 2,032
13년 전 조회 2,731
13년 전 조회 2,001
13년 전 조회 3,183
13년 전 조회 2,068
13년 전 조회 5,209
13년 전 조회 4,382
13년 전 조회 5,924
13년 전 조회 8,515
13년 전 조회 3,773
13년 전 조회 3,230
13년 전 조회 1,612
13년 전 조회 3,937
13년 전 조회 1,809
13년 전 조회 5,344
13년 전 조회 3,515
13년 전 조회 2,807
13년 전 조회 2,647
13년 전 조회 6,989
13년 전 조회 2,804
13년 전 조회 2,451
13년 전 조회 2,335
13년 전 조회 3,904
13년 전 조회 3,862
13년 전 조회 2,228
13년 전 조회 2,414
13년 전 조회 5,654
13년 전 조회 1,738
13년 전 조회 2,191
13년 전 조회 2,537
13년 전 조회 2,926
13년 전 조회 2,947
13년 전 조회 2,697
13년 전 조회 5,812
13년 전 조회 2,133
13년 전 조회 7,149
13년 전 조회 2,643
13년 전 조회 2,697
13년 전 조회 2,424
13년 전 조회 2,074
13년 전 조회 2,827
13년 전 조회 1,826
13년 전 조회 3,298
13년 전 조회 1,657
13년 전 조회 1,627
13년 전 조회 4,262
13년 전 조회 2,079
13년 전 조회 1,680
13년 전 조회 1,532
13년 전 조회 2,058
13년 전 조회 3,239
13년 전 조회 1,664
13년 전 조회 2,584
13년 전 조회 1,785
13년 전 조회 1,722
13년 전 조회 3,556
13년 전 조회 1,973
13년 전 조회 2,170
13년 전 조회 1,619
13년 전 조회 2,627
13년 전 조회 1,665
13년 전 조회 1,820
13년 전 조회 1,553
13년 전 조회 1,583
13년 전 조회 1,586
13년 전 조회 1,685
13년 전 조회 2,834
13년 전 조회 1,843
13년 전 조회 1,939
13년 전 조회 2,853
13년 전 조회 2,005
13년 전 조회 3,638
13년 전 조회 1,518
13년 전 조회 4,186
13년 전 조회 1,641
13년 전 조회 4,458
13년 전 조회 2,001
13년 전 조회 1,993
13년 전 조회 2,322
13년 전 조회 1,308
13년 전 조회 1,377
13년 전 조회 2,359
13년 전 조회 2,903
13년 전 조회 1,626
13년 전 조회 1,387
13년 전 조회 1,517
13년 전 조회 1,501