Benny's Blog
Navigation: Home » Archives for JavaScript
22. April 2012

The syntax of JavaScript and PHP is very similar. How similar is shown by the following function that I wrote to generate a random password:

PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password = $password.chr(rand(48,57));
    else if($func == 1)
      $password = $password.chr(rand(65,90));
    else if($func == 2)
      $password = $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
echo($randomPassword);
?>

JavaScript code:

<script type="text/javascript" line="1">
function rand(min,max){
  if (arguments.length === 0){
    min = 0;
    max = 32767;
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
 
String.prototype.chr = function(code){
  return String.fromCharCode(code);
}
 
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password += $password.chr(rand(48,57));
    else if($func == 1)
      $password += $password.chr(rand(65,90));
    else if($func == 2)
      $password += $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
document.write($randomPassword);
</script>
21. April 2012

The lower case letter “a” has ASCII code “97″ and can be generated in PHP like this:

<?php
  $a = chr(97);
  echo $a;
?>

In JavaScript, the same can be accomplished by:

<script type="text/javascript">
  var a = String.fromCharCode(97);
  document.write(a);
</script>

And this is how it works in Java:

public static void main(String[] args){
  String a = new Character((char) 97).toString();
  System.out.println(a);
}
20. April 2012

In PHP the rand() function returns a random number between a given minimum and maximum (by default 0 and 32767). The whole looks like this:

<?php
  $randomNumber = rand(1,100); // random number between 1 and 100
  echo $randomNumber;
?>

The same function can be rebuild with JavaScript:

<script type="text/javascript">
  function rand(min,max){
    if (arguments.length === 0){
      min = 0;
      max = 32767;
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
 
  var randomNumber = rand(1,100); // random number between 1 and 100
  document.write(randomNumber);
</script>
18. April 2012

To create a custom twitter button you need to encode some strings and insert a few lines of JavaScript. This is how it can be done:

HTML code:

<a id="twitterLink" target="_blank">
  <img src="./images/twitter.png" alt="Twitter button" />
</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

JavaScript code:

var url = window.location.href.split('?')[0];
var twitterUrl=encodeURIComponent(url);
var twitterText=encodeURIComponent('Hello World:');        
var twitterLink='https://twitter.com/share?text='+twitterText+'&url='+twitterUrl+'&count=none';
document.getElementById('twitterLink').href=twitterLink;
17. April 2012

Wer viele console.log() Einträge in seinem JavaScript-Code hat, sollte diese Einträge beim Live-Schalten der Webseite unbedingt entfernen. Um auf Nummer sicher zu gehen, dass beim Endanwender auch wirklich keine Log-Einträge zu sehen sind, empfiehlt es sich, die console.log()-Funktion zu überschreiben und somit unbrauchbar zu machen:

var console = {};
console.log = function(){};

Übrigens, Internet Explorer Version 8 und älter können mit console.log nicht umgehen und zeigen stattdessen nur Fehler an. Daher empfiehlt es sich immer, auf console.log in der Produktivumgebung zu verzichten.

16. April 2012

With JavaScript it is easy to get the current URL of the website. You can get the URL with and without parameters. This is how you can do it:

console.log(document.URL); // http://www.bennyn.de/sth/index.html?param=value
console.log(window.location.href); // http://www.bennyn.de/sth/index.html?param=value
console.log(window.location.href.split('?')[0]); // http://www.bennyn.de/sth/index.html
console.log(window.location.pathname); // /index.html