Onclick and window.location info

Making onclick work without using an html a tag

Page 1 of 1

2 Replies - 10694 Views - Last Post: 09 February 2009 - 09:24 PM

#1 wmello1019  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 09

Onclick and window.location info

Posted 07 February 2009 - 05:19 PM

Hi,
I am a new Javascript user and have a question i cant seem to get my teacher to explain to me (darn online classes). My problem is i am being instructed to add onclick="window.location='kidsnew.htm'" to the body of my file to create a link to another file. The instructions say i cant add an html a tag for the link and I'm not really sure how to make this work without it. Here is what I have so far....(note there are a bunch of other things i need to accomplish in this and i think i probably just dont have things in the right order and I'm not sure where the onclick tag needs to sit within the javascript sections)...

<html>
<head>
<title>Welcome To Kids Club</title>

<script language="Javascript">
<!--hide me

//get the date information
//


var today = new Date ();
var the_year = today.getYear();
var the_month = today.getMonth();
var the_day = today.getDate();
the_month = the_month + 1;
the_year = today.getFullYear();

//

var the_whole_date = the_month + "/" + the_day + "/" + the_year;

// show me -->

</script>

</head>


Todays Date is: 

<script language = "Javascript">
<!-- hide me

//write the date
//

document.write(the_whole_date);

// show me -->
</script>

<body>


<h1>Welcome To Kids Club</h1>

<script language = "Javascript">
<!-- hide me

//get the time information
//
var the_hour= today.getHours();
var the_minutes= today.getMinutes();
var the_seconds= today.getSeconds();

var the_whole_time = the_hour + ":" + the_minutes + ":" + the_seconds;
// show me -->

</script>

The Time is:

<script language = "Javascript">
<!-- hide me

//write the time
//

document.write(the_whole_time);

// show me -->
</script>

onclick="window.location='kidsnew.htm'"


<br>

<br>
<script language="Javascript">
<!-- hide me from old browsers

var browser_name=navigator.appName;		

var browser_version=parseFloat(navigator.appVersion);	  

alert("You're using " + browser_name + "version" + browser_version);

// show me -->


</script>

<br>

<br>
<h6 align="center"> <script language="Javascript">window.document.writeln("This document was last modified"+"" + document.lastModified)</script></h6>

</body>
</html>


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 08 February 2009 - 07:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Onclick and window.location info

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Onclick and window.location info

Posted 08 February 2009 - 07:37 PM

onclick is known as an event handler. It responds to things the users do like clicking a button on a form or page. When the user clicks the button for instance it then triggers the code in the onclick event to execute. These event handlers are tied to elements as attributes or through adding listeners (which you will get into probably a long time from now).

So to go to another page for instance when a user clicks a button you would create something like this...

<input type="button" name="mybutton" value="click me" onclick="window.location='kidsnew.htm'"/>



When the user clicks the button above labeled "click me" it will activate the onclick handler attribute which executes the window.location='kidsnew.htm' line of code and goes to that page.

Other handlers you will see are things like onblur (for when a control loses focus), onchange (for when an option is changed on the control) and onsubmit (when a form is being submitted).

You can find a list of these event handlers and how they work on the w3schools.com website below...

W3Schools.com Event Reference for Javascript

Enjoy!

"At DIC we be javascript event handling code ninjas... in any event we handle everything. That is what makes us so leet." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 wmello1019  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 09

Re: Onclick and window.location info

Posted 09 February 2009 - 09:24 PM

Thank you very much for your help. I knew there was something missing in the instructions that were provided. You are a life saver (and a good flavored one at that) :o)



View PostMartyr2, on 8 Feb, 2009 - 06:37 PM, said:

onclick is known as an event handler. It responds to things the users do like clicking a button on a form or page. When the user clicks the button for instance it then triggers the code in the onclick event to execute. These event handlers are tied to elements as attributes or through adding listeners (which you will get into probably a long time from now).

So to go to another page for instance when a user clicks a button you would create something like this...

<input type="button" name="mybutton" value="click me" onclick="window.location='kidsnew.htm'"/>



When the user clicks the button above labeled "click me" it will activate the onclick handler attribute which executes the window.location='kidsnew.htm' line of code and goes to that page.

Other handlers you will see are things like onblur (for when a control loses focus), onchange (for when an option is changed on the control) and onsubmit (when a form is being submitted).

You can find a list of these event handlers and how they work on the w3schools.com website below...

W3Schools.com Event Reference for Javascript

Enjoy!

"At DIC we be javascript event handling code ninjas... in any event we handle everything. That is what makes us so leet." :snap:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1