1

I have a Javascript imported a snow.js file that displays "Snow falling" across the screen for when it's winter. But If I choose a summer theme on the same page it will still display the Snow falling.

<script src="js/snow.js" type="text/javascript"><script>

Is their a way to create a Javascript code so if i click on a summer theme it will remove/comment the imported file and add/re-move commenting on the imported file when I click on a winter theme?

Edit:

I am using snowstorm.JS plugin realized I can call a function "snowStorm.toggleSnow()" via script. But I need to adjust it, so if they click on my css button "Summer" whilst on the theme summer it will do nothing.

I created this Jquery/JS code but it doesn't work. I believe it's the brackets but not sure. It should solve the problem.

Update:

I got it working, so If the user clicks a button it will "Toggle" the snowstorm effect on and off.

$("#Summer").click(function(){

    snowStorm.toggleSnow()


});

$("#Winter").click(function(){

    snowStorm.toggleSnow()



});

Is there a way of introducing if loops, so if the user clicks on the same button, eg: ID "Summer" whilst on the same ID/CSS "Summer" it will not toggle the snowstorm?

Zeon
  • 327
  • 3
  • 18
  • 1
    More likely than not, "snow.js" is actually a plugin which allows you to disable the effect via a JS API. No need to remove/reload the script. Can you point to the plugin you are using? – Igor Raush Oct 30 '15 at 23:37
  • You will have to add additional information on snow.js, to be able to stop its effect. – kemicofa ghost Oct 30 '15 at 23:45
  • Thanks @Grimbode . It's going to be my first time trying to disable a plugin via style switching. Have you got any tips / advice on how I should approach it? – Zeon Oct 30 '15 at 23:55

2 Answers2

0

Use JS or jQuery to remove the tag.

For example, in jQuery:

$("#summerThemeButton").click(function() {
    $("script[src=js/snow.js]").remove();
});

However, as @IgorRaush commented, there's probably a better way to change the theme (probably by a JS API) than to remove the script for it completely.

EDIT

As the comments suggest, it's not so easy to remove a JS file by removing the <script> tag. It is already executing code. The best way is to use the functions that came with it to stop it. Maybe you want to check if there are functions, such as:

var stopSnow = function() { /* ... */ };
var changeTheme = function(newTheme) { /* ... */ };

If there are truly no API functions (if you found an API; if you wrote it yourself just write a function!) then your best bet might be to remove all relevant/global variables and functions:

// for example, if you had a running function `snow()` or a global variable to make it snow:
snow = null;
theme = null;

Sorry there's no better way (I know of) to remove a script.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
0

you can use $.getScript in jQuery to conditionally load the file. See more at http://api.jquery.com/jQuery.getScript/

Saransh Kataria
  • 1,447
  • 12
  • 19