?
Solved

calling a function in On_Click event

Posted on 2004-04-13
5
Medium Priority
?
320 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Suggested Courses

850 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