Solved

calling a function in On_Click event

Posted on 2004-04-13
5
288 Views
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?")
if(answer)
var win2= window.open ("http://www.msn.com", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")
}
//-->
</script>
<a href="#", onclick=show_it()> MSN's home page</a>


If you would like to see more, here is my page:
http://www.geocities.com/rockatony/page2.html


Thanks,
Tony
0
Comment
Question by:Tony_A
5 Comments
 
LVL 1

Expert Comment

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

Expert Comment

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

var win2= window.open ("http://www.msn.com", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")

to

window.open ("http://www.msn.com", "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200")


BR MaB
0
 

Accepted Solution

by:
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)
window.open (location, "second",
" location=yes, resizable=yes, height=200, menubar=yes, toolbar=yes, scrollbars=yes, width=200");
}
//-->
</script>

-------------

You will only have that script in your page once.

Then for each link, you would just do this:

------------

<a href="#", onclick=show_it('http://www.msn.com')> MSN's home page</a>

-------------

hope that helps :)
0
 

Author Comment

by:Tony_A
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!
Tony
0
 

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

831 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