最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing external js file dynamically - Stack Overflow

programmeradmin2浏览0评论

I am trying to remove the js file on mouseclick. I am referring to www.javascriptkit/javatutors/loadjavascriptcss2.shtml for removing But its not working for me This is my corresponding code

HTML part:

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

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);
    }
}

I am trying to remove the js file on mouseclick. I am referring to www.javascriptkit./javatutors/loadjavascriptcss2.shtml for removing But its not working for me This is my corresponding code

HTML part:

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

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);
    }
}
Share Improve this question asked Apr 18, 2014 at 6:18 user3535625user3535625 521 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Do not quite understand your problem, but if you need to delete a file from DOM document then add to script tag attribute id and remove.

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

Try this i tested this

$(document).ready(function(){

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


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

    });
});

Try it in HTML file,js file gets removed when clicked on image

<!doctype html>
<html lang="en">
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="http://code.jquery./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/style/images/wp-header-logo.png?1"/></p></div>
</div>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论