1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I load a page or file after this javascript is complete?

Discussion in 'JavaScript' started by wallarookiller, Oct 15, 2013.

  1. #1
    I've tried to figure this out for an hour now, but it's pretty obvious I don't know Javascript.

    Basically this is a countdown script and after it finishes I'd like it to load an mp3 or a page.

    It even says where I can add the code, but I'm just not sure what to do. All help is appreciated!

    Here's the code:

    You can see where it says
    I figure I should be able to do this.

    
    <form>
    Minutes: <input type="text" id="mns" name="mns" value="0" size="3" maxlength="3" /> &nbsp; &nbsp; Seconds: <input type="text" id="scs" name="scs" value="0" size="2" maxlength="2" />&nbsp;
    <input type="button" id="btnct" value="START" onclick="countdownTimer()"/>
    </form>
    Countdown Timer: &nbsp; <span id="showmns">00</span>:<span id="showscs">00</span>
    <script type="text/javascript"><!--
        /* Function to display a Countdown timer with starting time from a form */
    // sets variables for minutes and seconds
    var ctmnts = 0;
    var ctsecs = 0;
    var startchr = 0;      // used to control when to read data from form
    
    function countdownTimer() {
      // http://coursesweb.net/javascript/
      // if $startchr is 0, and form fields exists, gets data for minutes and seconds, and sets $startchr to 1
      if(startchr == 0 && document.getElementById('mns') && document.getElementById('scs')) {
        // makes sure the script uses integer numbers
        ctmnts = parseInt(document.getElementById('mns').value) + 0;
        ctsecs = parseInt(document.getElementById('scs').value) * 1;
    
        // if data not a number, sets the value to 0
        if(isNaN(ctmnts)) ctmnts = 0;
        if(isNaN(ctsecs)) ctsecs = 0;
    
        // rewrite data in form fields to be sure that the fields for minutes and seconds contain integer number
        document.getElementById('mns').value = ctmnts;
        document.getElementById('scs').value = ctsecs;
        startchr = 1;
        document.getElementById('btnct').setAttribute('disabled', 'disabled');    // disable the button
      }
    
      // if minutes and seconds are 0, sets $startchr to 0, and return false
      if(ctmnts==0 && ctsecs==0) {
        startchr = 0;
        document.getElementById('btnct').removeAttribute('disabled');    // remove "disabled" to enable the button
    
        /* HERE YOU CAN ADD TO EXECUTE A JavaScript FUNCTION WHEN COUNTDOWN TIMER REACH TO 0 */
    
        return false;
      }
      else {
        // decrease seconds, and decrease minutes if seconds reach to 0
        ctsecs--;
        if(ctsecs < 0) {
          if(ctmnts > 0) {
            ctsecs = 59;
            ctmnts--;
          }
          else {
            ctsecs = 0;
            ctmnts = 0;
          }
        }
      }
    
      // display the time in page, and auto-calls this function after 1 seccond
      document.getElementById('showmns').innerHTML = ctmnts;
      document.getElementById('showscs').innerHTML = ctsecs;
      setTimeout('countdownTimer()', 1000);
    }
    //-->
    </script>
    
    Code (markup):
     
    Solved! View solution.
    wallarookiller, Oct 15, 2013 IP
  2. #2
    What you are looking for is window.location.href -- you set that to the URL you want to go to, and poof, away it goes.

    That said, I'd suggest dragging your markup kicking and screaming into the world of semantics with some proper hooks, and STOP using nyetscape 4 style references to element by name... likewise I'd add the counter in the script, removing the form elements once you no longer need them.

    <form id="countdown">
    	<fieldset>
    		<label for="countdownMinutes">Minutes:</label>
    		<input type="text" id="countdownMinutes" value="0" size="3" maxlength="3" />
    		<label for="countdownSeconds">Seconds:</label>
    		<input type="text" id="countdownSeconds" value="0" size="2" maxlength="2" />
    		<input type="submit" id="countdownSubmit" value="START" />
    	</fieldset>
    </form>
    Code (markup):
    I'd also suggest putting all your scripting hooks in the scripting, and some range checks wouldn't hurt either. Likewise you'll want to store the interval handler so that just in case you don't end up in a endless loop after setting the new location. (though with 1 second intervals it's unlikely). Putting it all in an anonymous function so you don't have to worry about cross-talk between scripts is probably a good idea too.

    (function(d, launchURI){
    
    	var
    		cDown = 0,
    		cHandler,
    		cShow,
    		cForm = d.getElementById('countdown'),
    		cMinutes = d.getElementById('countdownMinutes'),
    		cSeconds = d.getElementById('countdownSeconds'),
    		cSubmit = d.getElementById('countdownSubmit'),
    		cFieldset = cSubmit.parentNode;
    		
    	function show(v) {
    		while (cShow.firstChild) cShow.removeChild(cShow.firstChild);
    		cShow.appendChild(d.createTextNode(v));
    	}	
    	
    	function showCount() {
    		var m = Math.floor(cDown / 60), s = cDown % 60;
    		show((m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s);
    	}
    	
    	function update() {
    		if (--cDown > 0) showCount(); else {
    			show('Launching ' + launchURI);
    			clearInterval(cHandler);
    			window.location.href = launchURI;
    		}
    	}
    	
    	cForm.onsubmit = function(e) {
    		var t, eDIV, eUL, eLI, error = [];
    		e = e || window.event;
    		if (isNaN(cMinutes.value)) error.push('Minutes must be a number');
    		if (isNaN(cSeconds.value)) {
    			error.push(' Seconds must be a number');
    		} else if (cSeconds.value > 60) {
    			error.push('Seconds must be less than 60');
    		}
    		if (eDIV = d.getElementById('cDownError')) cFieldset.removeChild(eDIV);
    		if (error.length) {
    			eDIV = cFieldset.insertBefore(
    				d.createElement('div'),
    				cSubmit.nextSibling
    			);
    			eDIV.id = 'cDownError';
    			eDIV.appendChild(d.createTextNode(
    				'Cannot countdown, the following errors were encountered'
    			));
    			eUL = eDiv.appendChild(d.createElement('ul'));
    			for (t = 0; t < error.length; t++) {
    				eLI = eUL.appendChild(d.createElement('li'));
    				eLI.appendChild(d.createTextNode(error[t]));
    			}
    		} else {
    			cForm.removeChild(cFieldset);
    			cForm.appendChild(d.createTextNode('Countdown Timer:'));
    			cShow = cForm.appendChild(d.createElement('span'));
    			cDown = (cMinutes.value * 60) + Number(cSeconds.value);
    			showCount();
    			cHandler = setInterval(update, 1000);
    		}
    		if (e.stopPropagation) e.stopPropagation();
    		if (e.preventDefault) e.preventDefault();
    		e.cancelBubble = true;
    		e.returnValue = false;
    		return false;
    	}
    	
    })(document, 'http://www.google.com');
    Code (markup):
    Notice I also go WAY overboard preventing the submit, and latch onto the submit method for handling it.

    Hope this helps. Since NONE of this works without scripting, I'd be tempted to move the entire form into the scripting too, naturally with NOSCRIPT for people who either don't have or intentionally block scripting...
     
    deathshadow, Oct 16, 2013 IP
  3. wallarookiller

    wallarookiller Active Member

    Messages:
    647
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #3
    To be honest that is mostly half gibberish to me, but it works great and looks way cleaner. Thanks a lot. Really, thanks!
     
    wallarookiller, Oct 16, 2013 IP