Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem: href="#" sends request to server 2

Status
Not open for further replies.

AkinikA

Programmer
Jan 9, 2009
2
IL
Hi All,

I have a link with href="#".
The purpose of this link is to do an action on click.
Actual behavior: it sends a request to server and the page is refreshed..

This link is located inside HTML form, in a big complicated page.
I know that it can be solved by changing to href="javascript:void()", but href is generated automatically[/color red] by a 3rd party tool, and I cannot update it[/color red]

Code:
....
<a href="#" onclick="alert('123');">123</a>
....

Please advice why it happens, and how I can solve it
 
With JavaScript you should be able to get all the forms child elements, find the <a> tag then alter the onclick event.

e.g.
Code:
var childNodes = document.forms[0].childNodes;
for (i = 0; i < childNodes.length; i++) {
  if (childNodes[i].nodeType == 'a') {
    childNodes[i].onclick = childNodes[i].onclick + ' return false; '
    break;
  }
}

I haven't tested this, so it may need a tweak. Bear in mind that this finds the first <a> tag in the first form and modifies it, so you'll need to customise it to your needs.
 
Hi

Just a few words.

[ul]
[li]no need to process all [tt]Node[/tt]s, with [tt]getElementsByTagName('a')[/tt] you can filter out your target set of elements from the beginning[/li]
[li][tt]nodeType[/tt] is a numeric property of the [tt]Node[/tt], indicating its... well, type[/li]
[li]if you confused it with [tt]nodeName[/tt], then your code will work, but only for XHTML, as in HTML the element's name is returned in uppercase[/li]
[li]you are handling the [tt]onclick[/tt] as a string, but that is a function[/li]
[li]no need for [tt]break[/tt] as last instruction of a loop[/li]
[/ul]


Feherke.
 
Thank you for you ideas, I will try this.

Do you know this problem occurs?
On most sites href="#" has no effect, and in mine it sends request to a server..
Do you know why it could be?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top