动态删除外部js文件

时间:2014-04-18 06:18:58

标签: javascript jquery html html5

我试图在mouseclick上删除js文件。我指的是www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml删除 但它不适合我 这是我的相应代码

HTML部分:

<div id="dropdownlist" style="position:absolute;z-index:102;">
                  <a href="#" onclick="DisplayDropdownSelectedPage('blog')">BLOG</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('careers')">CAREERS</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('whistleblower')">WHISTLE BLOWSER</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('disclaimer')">DISCLAIMER</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('sitemap')">SITEMAP</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('feedback')">FEEDBACK</a><br/>
                </div>

JS部分:

function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 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(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

function DisplayDropdownSelectedPage(dropdownname)
{
    var width=$(window).width();
    var height=$(window).height();
    var top=document.getElementById("globelHead").offsetHeight;
    var animateheight=(height-top);
    var animatewidth=(width/2);
    var shiftright=(-(animatewidth)+"px");
    if(dropdownname=="blog")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Blog").show().animate({right:'0%'},1200);
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();

    }
    else if(dropdownname=="careers")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Careers").show().animate({right:'0%'},1200);
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();
    }
    else if(dropdownname=="whistleblower")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#WhistleBlower").show().animate({right:'0%'},1200);
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();
    }
    else if(dropdownname=="disclaimer")
    {   
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Disclaimer").show().animate({right:'0%'},1200);
        $("#SiteMap").hide();
        $("#Feedback").hide();

    }
    else if(dropdownname=="sitemap")
    {

        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#SiteMap").show().animate({right:'0%'},1200);
        $("#Feedback").hide();

    }
    else if(dropdownname=="feedback")
    {
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Feedback").show().animate({right:'0%'},1200);
    }
}

2 个答案:

答案 0 :(得分:3)

不太了解您的问题,但如果您需要从DOM文档中删除文件,请添加到脚本标记属性ID并删除。

<script id="mustBeRemoved">....
$("#mustBeRemoved").remove();

答案 1 :(得分:1)

试试这个我测试了这个

$(document).ready(function(){

 $('#cats').click(function(){  


        $('head script[src*="jquery-ui.js"]').remove();

    });
});

在HTML文件中尝试,单击图像时删除js文件

<!doctype html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function(){

 $('#cats').click(function(){  


        $('head script[src*="jquery-ui.js"]').remove();

    });
});
</script>
<title>Birman Cats</title>
</head>

<body>
<h1>  Black Cat Superstitions</h1>
<div id="black">
<h2>  Folk Lore</h2>
<div id="cats">
<p><img src="http://s.w.org/style/images/wp-header-logo.png?1"/></p></div>
</div>
</body>
</html>