calling a function in On_Click event

Posted on 2004-04-13
Last Modified: 2008-02-01
Hello all! Here is my debut question to this forum :-P

Why does every link that I coded into my web page link back to the last one I coded in the page?

I have in HTML written some dialog. At the end of each paragraph I've placed a link inside the
On_Click event. It seems I call the same function on this On_Click event, which happens to be
the last one I coded in the page.

Any one know why this is happening?

Here is the function:
<script language="JavaScript" type=text/javascript>
function show_it()
var answer=confirm("Are you sure you wish to enter this site?")
var win2= ("", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")
<a href="#", onclick=show_it()> MSN's home page</a>

If you would like to see more, here is my page:

Question by:Tony_A

Expert Comment

ID: 10820926
I believe that behind the show_it() you need to return false. So:
onclick="show_it();return false;"

Expert Comment

ID: 10821345
You don't need to set the var win2 if this is the only way you use this script. Change

var win2= ("", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")

to ("", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")


Accepted Solution

scottarius earned 125 total points
ID: 10821494
Just took a look at your source, your problem is you are inserting the function before each link, you can only have one function on a page with the same name.

What you need to do is put your function in the <head> of the page and set it up to pass it the desired URL instead of having a different function for each link. Like so:


<script language="JavaScript" type=text/javascript>
function show_it(location)
var answer=confirm("Are you sure you wish to enter this site?")
if(answer) (location, "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200");


You will only have that script in your page once.

Then for each link, you would just do this:


<a href="#", onclick=show_it('')> MSN's home page</a>


hope that helps :)

Author Comment

ID: 10824474
thanks to all of you for your answers. Scottarius hit the grand slam however!
after just reading your response I was ready to give you an A. I settled down,
replaced the code how you recommended and its flawless! :-)

Thanks bunches!

Expert Comment

ID: 10826022
glad i could help, thanks for the grade :D

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I put a code without running my other code 4 32
Glyph icons in Bootstrap 3 4 37
Create html table using xsl 8 28
form button worked now it doesnt anymore 9 39
Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
Find out what you should include to make the best professional email signature for your organization.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now