0

I used

<div id="myJSDiv"><script>...</script></div>
$("#myJSDiv").html('');

and

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

but nothing removed.

JS script is

<script id="count_down">
        var ss = 150;
        function countdown() {
            ss = ss-1;
            if (ss<0) {
                window.location="google.com";
            }else {
                document.getElementById("countdown").innerHTML=ss;
                window.setTimeout("countdown()", 1000);
            }
        }
        </script>
3
  • 4
    What problem are you trying to solve? Commented Mar 17, 2013 at 16:37
  • 1
    what do you expect to happen when you remove the script tag? Commented Mar 17, 2013 at 16:37
  • possible duplicate of Cleaning javascript variable scope by removing it's code from DOM Commented Mar 17, 2013 at 16:44

1 Answer 1

3

Once a script has loaded, removing the script tag it came from does not do anything to the functions or variables defined or created by that script.

There is no way to remove a block of code like that after it has loaded. Global functions can be redefined after they were loaded, but that's about all you can do. Perhaps, if you described the overall problem you are trying to solve, we could suggest a better approach.


Now that you've added the actual problem to your question, to stop your countdown timer, you can do this:

 <script id="count_down">
    var ss = 150;
    var countdownTimer;
    function countdown() {
        ss = ss-1;
        if (ss<0) {
            window.location="google.com";
        }else {
            document.getElementById("countdown").innerHTML=ss;
            countdownTimer = window.setTimeout(countdown, 1000);
        }
    }

    function stopCountdown() {
        clearTimeout(countdownTimer);
    }
    </script>

Just call the stopCountdown() function when you want to stop the countdown timer.

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

4 Comments

I have a javascript count down script which I call on body onload(). There is a button on my html page, when I clicked that button it returns ether 0 or 1. I want if it return 0 the js script should be removed with out refresh the page.
@PHPFerrari - you CANT just remove scripts that have already been parsed and are already executing. If you want to stop the countdown script from running, then that can be done, but you'd have to show us the countdown script so we could instruct how to stop it from running.
@PHPFerrari - I added to my answer how you can stop the countdown timer.
@PHPFerrari - I don't know what you mean by "varnished some heading tags" and whether that's a question you're asking about.

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.