2/11/2007

Href Vs onClick : Anchor tag

< a href="javascript:someFuntion()" > and <
a href="#" onclick="javascript:someFunction()" /> There are many searches that would result in this topic if u #Ooooooogle it..!

I had problems using these functions recently.

I've a common function that would add a popup button to the screen like this,

document.write('< a href="'+href+'" > < img id="'+nam+'_img" border="0" title="'+title+'" src="'+image+'" /> </a >');

The above method was not working when used in popups(showModalDialog). Later i changed the function to return,

document.write('< a href="#" onclick='"+ href +"'> < img id="'+nam+'_img" border="0" title="'+title+'" src="'+image+'"
/> < /a >');

Eurekha it worked...but Why...? Am still to find out the hidden evil in this!

5 comments:

dev said...

I could not understand what you are trying to do.
As per my understanding href="someURL" takes you to a new page/URL since it is a hyper link.
I believe that in order to keep the user continue in the current page (avoid navigating to next one), and open a pop up screen you are trying to use
href="#" and onclick="somefunction()".
Assume that the client's browser doesn't support java script, the link fails.
So its better to avoid using "#".
Instead use
a href="link"
onclick="someFunction(this.href);return false;
I'm using return false because
the href will be executed only if the onClick returns true.
This is how it works.
Hope this should work.
Did i confuse you?
I hope i did not!!!
Refer
http://evolt.org/node/20938
tha above link it should be more useful.

Regards
Thinking Thin Thinking JAVA.

Dinesh said...

Dev,
Thanks for Thinking Thin!
The question was why it fails when this function was called from a popup...!

lemme b more clear, try to invoke this function from a popup and u could see href='someFunction' failing miserably. That is the browser(IE version 6) did not pick up this function when called from a popup..!

Keep Thinking Thin.

dev said...

oge..Is it so?
Post the exact code.Let me check what u r trying to do

Dinesh said...

Dev,
Sorry for making things complicated.

Here it goes..

buttons.js
-----------
function addPopupBtn(title, href, image, nam){
document.write('' + stmt + ' href="'+href+'">....+'');
}

fileOne.html
----------

addPopupBtn("Test", "javascript:callMeOne()","","test");
//Clicking the popup button would call the function callMeOne();

Now add a new window in the same page.
window.showModalDialog('../common/fileTwo.html.............);

fileTwo.html
-------------
addPopupBtn("Test", "javascript:callMeTwo()","","test");
//Clicking the popup button would call the function callMeTwo();


Problem
--------
Open fileTwo.html as a popup from fileOne.html. The function callMeTwo will not be called from the popup!

MLB2k11 said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
SEO tools

dzone.com