0

I want to add a script

 if(screensize>980){
  add <script src="somejs">// functions should work without time delay
}
if(screensize<980){
 remove  <script src="somejs">
}

when screen size is less than 900, and I want to remove the same script when the user resize the screen to bigger size. and then back again removing and adding the script dynamically based on the screen size. script dependent functions should work without any problem (dynamically) and without delay and reloading. is it possible. I tried so many things but none worked.

$.ajax, document.createElement("script") - I tried these two but i want it dynamically remove and add.

For example: add and remove <script src="somescript">. Is there a possibility to comment and uncomment the loaded script dynamically?

3

3 Answers 3

0

Removing and adding seems very cumbersome. Can't you simply add a check for the current screen size at the start of your script, and abort if it doesn't fit the parameters?

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

2 Comments

This should have been a comment, if you do not have enough reputation to comment, earn reputation. This kind of answer is likely to get you downvotes, pushing you even further away from being able to comment. And it's only 50 reputation anyhow. (It also displays this question as 'having an answer' on the homepage, making it less likely that people with actual knowledge of the answer will look at the question, making the question useless)
@somethingthere, thank you for your guidance. Is there a way for me to remove my answer so the question will appear unanswered?
0

I want to add a script when screen size is less than 900, and I want to remove the same script when the user resize the screen to bigger size. and then back again removing and adding the script dynamically based on the screen size.

Why not attach an Event Listener to the screen to watch out for when the screen size changes:

window.addEventListener("resize",addOrRemoveFunction,false);

Then write a function which determines the width of the screen size and adds or removes an Event Listener to myElement accordingly:

function addOrRemoveFunction() {

    if (window.innerWidth < 900) {
        myElement.addEventListener("click",myFunction,false);
    }

    else if (window.innerWidth > 899) {
        myElement.removeEventListener("click",myFunction,false);
    }
}

Then you can write whatever function you would like to have present or absent depending on the current screen width:

function myFunction() {

[Javascript here]

}

1 Comment

actually I want to add and remove <script src="somescript">
0

jQuery's $.getScript("your script location") will half do the job. Unloading, on the other hand, I doubt it. BUT if you've structured your scripts to be modularized, you can mimic it. Something like the below might work (UNTESTED):

var baseScript;
var scripts = {};

(function (module) {
    var _field = ...;
    var _privateFunction = function() {...};
    module.property = ...
    module.publicFunction = function () {...};
})(scripts.largeScreenScript);

(function (module) {
    ...
})(scripts.smallScreenScript);

if(screensize>980){
    baseScript = scripts.largeScreenScript;
}
if(screensize<980){
    baseScript = scripts.smallScreenScript;
}

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.