Solved

calling a function in On_Click event

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

744 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

13 Experts available now in Live!

Get 1:1 Help Now