This below JavaScript code will enable you to place a button on your website that, when clicked on, will make your web site your visitors home page.
<INPUT TYPE="button"
VALUE="Make This
page Your Home Page" onClick="this.style.behavior='url(#default#homepage)';
this.setHomePage('http:// www.usetricks.com');">
Display:
There are three location
where JavaScript can be placed for use in a webpage.
- within the head tag
- Within the body tag
- In an external file
If you want to have a
script run if some event occure,like when a user clicks a button, then you will
place that script within the head tag. If you want the script to run when the
page loads, then you will have to place the script within the body tag.We use
External JavaScript files if we want seprate or hide javascript function from html webpage .
An Example Placing Script within head Tag
<html>
<head>
<script type="text/JavaScript">
<!--
function popup() {
alert("Hello I am within Head")
}
//-->
</script>
</head>
<body>
<input type="button"
onclick="popup()"
value="popup">
</body>
</html>
An Example Placing Script within <body> Tag
<html>
<head>
</head>
<body>
<script type="text/JavaScript">
<!--
alert("Hello I am within Head")
//-->
</script>
</body>
</html>
An Example Placing Script within External JavaScript files
<html>
<head>
<script type="text/JavaScript"
src=”script.js”>
</script>
</head>
<body>
<input type="button"
onclick="popup()"
value="popup">
</body>
</html>
Place the following code in script.js
file .
function popup()
{
alert("Hello I am within External file");
}
This javascript code will display the current
time from the client’s computer. This will display the format you might see on
a typical digital clock -- HH:MM AM/PM (H = Hour, M = Minute).
<h4>The Current Time Is
<script type="text/javascript">
<!--
var dateobj = new Date()
var hrs =
dateobj.getHours()
var min =
dateobj.getMinutes()
if (min <
10){
min = "0" + min
}
document.write(hrs + ":" + min + "
")
if(hrs >
11){
document.write("PM")
} else
{
document.write("AM")
}
//-->
</script>
</h4>
Display:
The Current Time Is
The getDate() method will returns the day of the month (from
1 to 31) for the specified date, according to local system time.It is always
use with the Javascript Date object.
Sysntax is
Date.getDate();
This method is supported by all main browser.
Example:
Return the day of the month:
<script type="text/javascript">
var MyDate=new Date();
document.write(MyDate.getDate());
</script>
<!--Output will be Date of your
local system-->
Return the day of the month from a specific date:
<script type="text/javascript">
var MyDate=new Date("May 25,
2012 02:25:00");
document.write(MyDate.getDate());
</script>
<!--Output will be 25-->
To view webpages which uses JavaScript, you need to ensure that your browser has JavaScript enabled. if JavaScript is disable, you can still view the webpage without JavaScript, but you will not be able to take advantage of the JavaScript functionality.
How do you check if your browser has JavaScript enabled?
This will depend on the browser you're using. Steps for some of the more common browsers are below:
Internet Explorer (6.0):
- Go to Tools from the top menu
- Select Internet Options
- Click on the Security tab
- Click Custom Level
- Scroll down until you see the Scripting section
- Ensure that the Active Scripting option is set at Enabled
- Click OK
Netscape Navigator (4.8):
- Go to Edit from the top menu
- Select Preferences
- Select Advanced
- Select Scripts & Plugins
- Check the Enable JavaScript checkbox
- Click OK
Mozilla Firefox (1.0):
- Go to Tools from the top menu
- Select Options
- Select Web Features from the left menu
- Ensure the Enable JavaScript option is checked
- Click OK
Mozilla Firefox (1.5):
- Go to Tools from the top menu
- Select Options
- Click on the Content button
- Ensure that the Enable JavaScript option is checked
- Click OK
Apple Safari (1.0):
- Go to Safari from the top menu
- Select Preferences
- Select Security
- Ensure that the Enable JavaScript option is checked
- Click OK
If you are a developer and you had develope web pages with JavaScript, then it is good practice to view your website with JavaScript disabled. This will show you what your website will look like to users who choose to disable JavaScript.
Video tutorial about enabling JavaScript in one of the most commonly used web browsers: Google Chrome
How To Turn Javascript On/Off IE
Video tutorial about enabling JavaScript in one of the most commonly used web browsers: Safari.
How to enable JavaScript in Mozilla Firefox
HTML, JavaScript Scripting, ,Scripts, Java, WebBrowser, browser