Tag 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 – Return to Previous URL

The following Javascript function will redirect you to the previous URL when it is called.

function goToPreviousPage() {
  history.go(-1)
}

 

Add a return button to your HTML page as follow.

<input type="button" name="returnButton" value="Return" onclick="history.go(-1);"/>

 

Done =)

Reference: Back to previous page

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

Drupal – Add Javascript in Theme

As discussed before, we can add Javascript/JQuery in Drupal modules.
Drupal – Create Javascript for Module

So if the Javascript/JQuery is related to the layout and independent of the module, we should add it in the theme instead. For example, i want to have a alert box whenever a page is loaded, i should then add it to my current Drupal theme. The steps are listed below.

1. Open you current theme folder and add the following line in the theme.info.

  • scripts[] = js/theme.js

Continue reading Drupal – Add Javascript in Theme

Drupal – Add Javascript in Module

In Drupal, you can add your Javascript to module or theme. If the javascript is related to the module logic and independent of the theme, we should add the .js file in the module. Moreover, Drupal already includes the JQuery library. So we can use the JQuery functions in the .js file.

Let’s recall the custom module we have created in previously.
Drupal – Create a Block

i want to show an Javascript alert box whenever the block is shown. Let’s create a custom.js in <drupal_root>/sites/all/custom.

jQuery(document).ready(function() {
	alert("Hello World");
});

Continue reading Drupal – Add Javascript in Module

jQuery – Avoid Conflict with other Javascript Libraries

Recently start learning jQuery =)

When you are using jQuery with other Javascript libraries such as Prototype. There will be conflict on the $() function. There are 3 ways to bypass the conflict.

1. Only use jQuery() for the jQuery calls and revert the $() to Prototype

jQuery.noConflict();
   
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
	jQuery("div").hide();
});

// Use Prototype with $(...), etc.
$('someid').hide();

Continue reading jQuery – Avoid Conflict with other Javascript Libraries