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
Solved

calling a function in On_Click event

Posted on 2004-04-13
5
294 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

828 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