Category Archives: Javascript

Javascript – HTML Redirect

In PHP, we can use the header() function to redirect the URL.
In HTML, we can use the meta tag to redirect the URL.
For more information, visit PHP/HTML – Redirect URL in Seconds

But sometimes, we want the redirection starts after the HTML body is loaded. This can be done by Javascript. Continue reading Javascript – HTML Redirect

Javascript – Passing Parameters for the Function in setTimeout()

Previous post: Javacript – Schedule a Function call by setTimeout()

If the delayed function has input parameters, setTimeout() would fail when you simply add the parameters in the setTimeout() call.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    /* The call will fail and the alert box will display immediately */
    setTimeout(delayedAlert("Hi Eureka!"), 3000);
  });

  function delayedAlert(str) {
    alert(str);
  }
</script>

Continue reading Javascript – Passing Parameters for the Function in setTimeout()

jQuery & Javascript – Schedule a Function call by setTimeout()

In Javascript, you could schedule a function call using the setTimeout().

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        setTimeout(delayedAlert, 3000);
      });
		
      function delayedAlert() {
        alert("Hello Eureka!");
      }
    </script>
  </head>
  <body>
    <h1>Eureka!</h1>
  </body>
</html>

Continue reading jQuery & Javascript – Schedule a Function call by setTimeout()

Javascript – Convert Number to String and Vice Versa

Convert String to Number using parseFloat() or parseInt()

/* parseFloat() */
parseFloat('1.45kg')  // 1.45
parseFloat('77.3')    // 77.3
parseFloat('077.3')   // 77.3
parseFloat('0x77.3')  // 0
parseFloat('.3')      // 0.3
parseFloat('0.1e6')   // 100000

/* parseInt() */
parseInt('123.45')  // 123
parseInt('77')      // 77
parseInt('077',10)  // 77
parseInt('77',8)    // 63  (= 7 + 7*8)
parseInt('077')     // 63  (= 7 + 7*8)
parseInt('77',16)   // 119 (= 7 + 7*16)
parseInt('0x77')    // 119 (= 7 + 7*16)
parseInt('099')     // 0 (9 is not an octal digit)
parseInt('99',8)    // 0 or NaN, depending on the platform
parseInt('0.1e6')   // 0
parseInt('ZZ',36)   // 1295 (= 35 + 35*36)

Continue reading Javascript – Convert Number to String and Vice Versa