0

Hi I am new in programming and I coulden't get my code working so please help me.

I want to show a side via iframe, when the counter is counting down to a date an an other one if the counter reached zero (Expired).

Here is my code:

    <script language="javascript" type="text/javascript" src="counter.js"></script>
<div id="countdown"></div>
<iframe id="site" src="http://google.com" width="90%" height="400" name="Site">
  <p>Your Browser doesn't support iframe: Please click <a href="http://google.com">here</a></p>
</iframe>
<script>
var test = document.getElementById("countdown");

if (test = "Expired") {
    return;
}
else {
    document.getElementById("site").src = "http://youtube.com";
    document.getElementById("site").herf = "http://youtube.com";
}
</script>

The counter.js is working fine but the second script isn't. It alwasy jumps to expired even if the counter is running.

zero
  • 13
  • 2
  • Why are You comparing `test` object to `Expired` string? – Jazi Dec 03 '14 at 08:23
  • Because the output of the counter is Expired if it reached zero but if you have a other idea please let me know. – zero Dec 03 '14 at 08:27
  • From where do You have this counter? Or did You wrote it yourself? – Jazi Dec 03 '14 at 08:30
  • The script is from the first answer oft this post http://stackoverflow.com/questions/9335140/how-to-countdown-to-a-date – zero Dec 03 '14 at 08:42

2 Answers2

0
  1. Use == or === to compare values. test = 'Expired' is an assignment expression, which returns the assigned value after the assignment. So the if statement is actually if("Expired"), and string "Expired" is a truthy value, finally it becomes if(true), the check always goes to return. Should be: if(test == 'Expired')

  2. I didn't use counter.js, but in test = document.getElementById("countdown"), the variable test is either null or a DOM element object, if should never be equal to string "Expired". You may need to check out the doc of counter.js, to find the right usage of the lib. Maybe something like (I guess) test.innerHTML === "Expired".

Leo
  • 13,428
  • 5
  • 43
  • 61
0

Check this:

jsFiddle version: http://jsfiddle.net/c87j2drr/

Code:

HTML:

<div id="countdown"></div>
<iframe id="site" src="http://onet.com" width="90%" height="400" name="Site">
    <p>Your Browser doesn't support iframe: Please click <a href="http://onet.com">here</a></p>
</iframe>

Javascript (Your function):

function countingEnd() {
     document.getElementById('countdown').innerHTML = 'Expired';
     document.getElementById("site").src = "http://wp.pl";
}

Javascript (modified counting code):

var end = new Date('12/03/2014 10:14 AM');

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;

     if (distance < 0) {
        clearInterval(timer);

        countingEnd();

        return;
     }

    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('countdown').innerHTML = days + 'days ';
    document.getElementById('countdown').innerHTML += hours + 'hrs ';
    document.getElementById('countdown').innerHTML += minutes + 'mins ';
    document.getElementById('countdown').innerHTML += seconds + 'secs';
}

timer = setInterval(showRemaining, 1000);
Jazi
  • 6,569
  • 13
  • 60
  • 92
  • Is it posible to get the actual time [var now = new Date()] with a php function from the serve without losing the live countdown? – zero Dec 03 '14 at 13:20
  • Jap I know that, but it´s taking the time from the client and I don't know if it's posible to import the time from the server with JavaScript with PHP you can du such a thing but it isn't posibel to see a live progress – zero Dec 03 '14 at 14:31
  • You can do this with AJAX, for example. Make a new request to a page which is generated by PHP and returns only a date (as a string, or json). Then, download it with JavaScript, parse it and throw to Your countdown JavaScript function. – Jazi Dec 03 '14 at 14:40
  • ok sorry but I just can do the step with the php something like `code` `code` but I have no idea to handle the rest – zero Dec 03 '14 at 14:45