Archive for the ‘JavaScript’ Category

Use multiple lines with document.write()

Posted: March 30th, 2010 | Author: Henrie Media Inc. | Filed under: JavaScript | No Comments »

I often get the question if it’s possible to use document.write() in JavaScript to output multiple likes instread of having to put – document.write(‘ – at the beginning of every line and – ‘); – at the end. The answer is Yes. Here is how -

document.write(
‘line1′
+’line2′
+’line3′
)

OR -

document.write(
‘line1′ + ‘\n’
+’line2′ + ‘\n’
+’line3′ + ‘\n’
)

..or use a single string -

document.write(
‘line1\
line2\
line3′
)

JavaScript is a little funny, so make sure you test! Hope this helps!

Print, Email, Share, Enjoy!
  • Print
  • email
  • Twitter
  • Facebook
  • Digg
  • MySpace
  • Mixx
  • del.icio.us
  • Technorati
  • StumbleUpon

Random numbers in JavaScript

Posted: March 25th, 2009 | Author: Henrie Media Inc. | Filed under: JavaScript | No Comments »

ComputerI am often asked the question of how to generate random numbers in JavaScript. It’s a little tricky and the results may be unpredictable, but the technique to getting them certainly is not. To generate a random number in JavaScript, simply use the following code:

var randomnumber=Math.floor(Math.random()*11)

where 11 dictates that the random number will fall between 0-10. To increase the range to, say, 100, simply change 11 to 101 instead.

Some of you may be curious as to why Math.floor(), instead of Math.round(), is used in the above code. While both successfully round off its containing parameter to an integer within the designated range, Math.floor does so more “evenly”, so the resulting integer isn’t lopsided towards either end of the number spectrum. In other words, a more random number is returned using Math.floor().

Print, Email, Share, Enjoy!
  • Print
  • email
  • Twitter
  • Facebook
  • Digg
  • MySpace
  • Mixx
  • del.icio.us
  • Technorati
  • StumbleUpon

JavaScript Timer

Posted: March 3rd, 2009 | Author: Henrie Media Inc. | Filed under: Code Library, JavaScript | No Comments »

ComputerSometimes you need to perform a function after a set time has passed. This script is a simple to use JavaScript timer.

Here’s the script:

<SCRIPT LANGUAGE = "JavaScript">
<!--
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 10
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        alert("You have just wasted 10 seconds of your life.")
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
//-->
</SCRIPT>

To use this script, copy everything between and including the <SCRIPT> and </SCRIPT> tags and insert it on your page between the </HEAD> and <BODY> tags.

The following line sets the length, in seconds, of the timer:

secs = 10

The code decreases the “secs” variable by 1 each second. When “secs” gets to 0, the clock is stopped and that’s when you do whatever it is you want to do once the timer is done (such as display a message or send the user to another page).

In the example above, I use a simple form button to start the timer (it also displays the countdown in the status bar). You could also start the timer (that is, run the InitializeTimer() function) automatically by adding the following to the <BODY> tag:

onLoad="InitializeTimer()"

Print, Email, Share, Enjoy!
  • Print
  • email
  • Twitter
  • Facebook
  • Digg
  • MySpace
  • Mixx
  • del.icio.us
  • Technorati
  • StumbleUpon

Bookmark this page JavaScript

Posted: May 27th, 2008 | Author: Henrie Media Inc. | Filed under: JavaScript | 3 Comments »

ComputerHere is a handy “Bookmark this page” JavaScript I wrote that will work in Internet Explorer, Firefox, Safari and most of the major web browser. The script will prompt the user to bookmark the page they are on. Here is what you need to use this script on your website.

Put the following code between the <head></head> tags of your webpage -

<script>
function bookmark(title,url){
if(window.sidebar)
window.sidebar.addPanel(title,url,”");
else if(window.opera && window.print){
var elem = document.createElement(‘a’);
elem.setAttribute(‘href’,url);
elem.setAttribute(‘title’,title);
elem.setAttribute(‘rel’,'sidebar’);
elem.click();
}
else if(window.external)
window.external.AddFavorite(url,title);
}
</script>

Put the following script in your Bookmark this page link (this can be a text or image link) -

<a href=”javascript:bookmark(‘Henrie Media Inc.’,’http://www.henriemedia/’);”>Bookmark this page</a>

Make sure you change the parameters (‘title’, ‘url’) to the ‘url’ you want to bookmark and the ‘title’ you want the bookmark to say. In my example the ‘url’ is “http://www.henriemedia.com” and the ‘title’ is “Henrie Media Inc.”

That’s it! Your are off and bookmarking. Please leave a comment if you find this post to be helpful.

Print, Email, Share, Enjoy!
  • Print
  • email
  • Twitter
  • Facebook
  • Digg
  • MySpace
  • Mixx
  • del.icio.us
  • Technorati
  • StumbleUpon