/    Sign up×
Community /Pin to ProfileBookmark

problem: href="#" sends request to server

Hi All,

I have a link with href=”#”.
[B]The purpose[/B] of this link is to do an action on click.
[B]Actual behavior:[/B] 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 [COLOR=”Red”]I cannot update the href [/COLOR](its an automatically generated code)

[code=html]
….
<a href=”#” onclick=”alert(‘123’);”>123</a>
….
[/code]

Please advice why it happens, and how I can solve it

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@ixxalnxxiJan 09.2009 — [code=html]....
<a href="javascript:null(0);" onclick="alert('123');">123</a>
....[/code]


This might do it, although I don't remember the syntax. If this doesn't work it's something very close, like javascript:null.
Copy linkTweet thisAlerts:
@nikaauthorJan 09.2009 — I cant change href, because the A tag is [U]generated automatically[/U] by a third party tool
Copy linkTweet thisAlerts:
@mintedjoJan 09.2009 — Which bit of the code CAN you change?
Copy linkTweet thisAlerts:
@nikaauthorJan 09.2009 — I can change everything except this link.

What causes this problem?
Copy linkTweet thisAlerts:
@A1ien51Jan 09.2009 — on page load or on dom ready, loop through the links on the page and alter the onclick events to cancel the click event.

Eric
Copy linkTweet thisAlerts:
@TheTeenScripterJan 10.2009 — umm... isn't there a way you can create your own custom code?
Copy linkTweet thisAlerts:
@jarcoalJan 10.2009 — Hi All,

I have a link with href="#".

[B]The purpose[/B] of this link is to do an action on click.

[B]Actual behavior:[/B] 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 [COLOR="Red"]I cannot update the href [/COLOR](its an automatically generated code)

[code=html]
....
<a href="#" onclick="alert('123');">123</a>
....
[/code]


Please advice why it happens, and how I can solve it[/QUOTE]


I don't really know why it's refreshing the page, any link pointing to a hash shouldn't cause a page refresh.

You could try something like this:

[CODE]<a href="#" onclick="return some_function()">123</a>

<script>
function some_function() {
alert("123");
return false; //returning false usually cancels the default browser behavior
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@felgallJan 10.2009 — Returning false will stop the default action from happening but if you never want the default action to happen then you'd be better off to use a tag that doesn't have a default to suppress.
Copy linkTweet thisAlerts:
@nikaauthorJan 13.2009 — Thank you for your replies

The "return false" solution didn't work.

I am trying to understand, why I have this 'unexpected' behavior in href="#":

on most sites href="#" has no effect, and in mine it sends request to a server..

Do you know why it could be? ?


thank you
Copy linkTweet thisAlerts:
@slaughtersJan 13.2009 — [code=html]<a href="#" onclick="alert('123'); return false;">123</a>[/code]Should work.
Copy linkTweet thisAlerts:
@nikaauthorJan 13.2009 — 
<a href="#" onclick="alert('123'); return false;">123</a>

Should work.
[/QUOTE]


well, it works with 'alert', and doesn't work when I invoke another function.

I am trying to understand the cause of problem, because I have many links like this.
Copy linkTweet thisAlerts:
@talldeanJan 13.2009 — well, it works with 'alert', and doesn't work when I invoke another function.

I am trying to understand the cause of problem, because I have many links like this.[/QUOTE]


Can you post a stripped down example that causes the problem in your browser?
Copy linkTweet thisAlerts:
@felgallJan 14.2009 — The only reason it wouldn't work with another function is if that other function is returning true and bypassing the return false.
×

Success!

Help @nika spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...