Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

More frequently sought solutions

Our esteemed columnist tackles 10 more of your most common JavaScript questions

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 6



<![if !supportEmptyParas]> <![endif]>
<SCRIPT>
function processMessage () {
showMessage ("Hello", "There");
}
<![if !supportEmptyParas]> <![endif]>
function showMessage () {
      var Temp = showMessage.arguments.caller.toString()
      Ret = Temp.substring (Temp.indexOf (" "), Temp.indexOf ("("))
      alert (Ret)
}
<![if !supportEmptyParas]> <![endif]>
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="Click" onClick="processMessage()">
<INPUT TYPE="button" VALUE="Click" onClick="showMessage()">
</FORM>


SUBHEAD: Play a script when starting Netscape You can play a script when first starting Netscape if you set the Start With option to point to a local home page that contains the JavaScript you want to use. For example, the following script displays a welcome message when the local home page is loaded:



<![if !supportEmptyParas]> <![endif]>
<HTML><HEAD>
<TITLE>Local Home Page</TITLE>
</HEAD>
<SCRIPT>
function greetings() {
      alert ("Howdy!")
}
</SCRIPT>
<BODY onLoad="greetings()">
More body stuff here...
</BODY></HTML>


To set the local home page in Netscape, follow these simple steps:



  1. Choose "Options," "General Preferences."
  2. Click on the Appearance tab, and choose Home Page Location for the Start With option.
  3. Fill in the path and name of the local home page you want. For example, on a PC you would use the syntax:


file://c|/javascript/homepage.htm



SUBHEAD: Link to one page for JavaScript browsers and another page for other browsers A variation on the previous theme is how to provide a single link that directs JavaScript browsers one way, and non-JavaScript browsers the other way. The solution is easy: simply provide the non-JavaScript link in the HREF= attribute of the link then change to the new URL for the onClick event handler. The following example links to the "nojs.htm" page for non-JavaScript browsers, and "yesjs.htm" for JavaScript browsers:



<![if !supportEmptyParas]> <![endif]>
<A HREF="nojs.htm" onClick="this.href='yesjs.htm'">Click</A>


SUBHEAD: Process a link before loading a new document The example shown here allows you to process a link before loading a new document. The technique uses the JavaScript: protocol as the HREF of the link to call a JavaScript function. In this case, the function is processLink, and the HREF appears as:



<![if !supportEmptyParas]> <![endif]>
<A HREF="JavaScript:processLink">


The processLink function contains an alert box that represents whatever processing you want to do before linking to a new URL. In the next line, the code sets the location property to the desired URL you want to go to, as shown here:



<![if !supportEmptyParas]> <![endif]>
<HTML>
<HEAD>
<TITLE>Process Link First</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function processLink (form)  {
      alert ("Process steps here");
      location = "somepage.html";
}
</SCRIPT>
</HEAD>
<BODY>
<A HREF="JavaScript:processLink()">Click here</A>
</BODY>
</HTML>


Note that when you place the mouse over the link, the status bar displays the HREF, which says "JavaScript:processLink()." One practical application of this approach is to ask the user if she really wants to link to a new page (or display a large graphic or sound). The processLink function would then contain:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources