Solved

calling a function in On_Click event

Posted on 2004-04-13
5
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

728 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