0

I do several similar things in several Js files. I want to deactivate some js files and enable others in another file within a conditional statement. How can I do this?

for example:

// My files: | javascript_1.js | - |  javascript_2.js |

if( flag == true ){
// disable javascript_1.js
}
else{
// enable javascript_2.js
}
5
  • 3
    Once a script has been loaded and parsed, it can't be removed or "disabled". What you can do, is to conditionally load your scripts, but I suspect your problem can be solved with functions. Commented Feb 10, 2021 at 6:35
  • 1
    There are plugins to disable a specific script I do not know how these work. But I did not test them and got to know them today superuser.com/questions/278590/… @Teemu Commented Feb 10, 2021 at 6:43
  • Yes, plugins can do that, and you can disbale a script in the DevTools, but it's not possible to do it on the code of a web page, because the script is not reachable to the DOM after it is parsed, you simply can't refer it with JS., Commented Feb 10, 2021 at 6:45
  • If you have a practical design problem you're trying to solve by enabling and disabling scripts, you might introduce your desing and ask for the specific problem, as this smells more like an xy-problem. Commented Feb 10, 2021 at 6:49
  • 1
    Okay, thank you for your answers. @Teemu Commented Feb 10, 2021 at 6:49

2 Answers 2

2

Try Using the following:

If you click Add Bootstrap bootstrap will be added dynamically. If you click Remove Bootstrap will be removed dynamically;

You can check by input field and button styles

function removejscssfile(filename, filetype){
    let targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; //determine element type to create nodelist from
    let targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; //determine corresponding attribute to test for
    let 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 createjscssfile(filename, filetype){
    let fileref;
    if (filetype=="js"){ //if filename is a external JavaScript file
        fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    document.getElementsByTagName("head")[0].appendChild(fileref);
}

function addB() {
    createjscssfile('https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css','css');
    createjscssfile('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js','js');
    createjscssfile('https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js','js');
}

function removeB() {
    removejscssfile('https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css','css');
    removejscssfile('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js','js');
    removejscssfile('https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js','js');
}
<button type="button" class="btn btn-success" onclick="addB()">Add Bootstrap</button>
<button type="button" class="btn btn-danger" onclick="removeB()">Remove Bootstrap</button>

<div class="mb-3">
  <label for="exampleFormControlInput1" class="form-label">Email address</label>
  <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]">
</div>

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1

refresh the page when you need new code from one file.

If you do not have a problem with page refresh.

location.reload();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.