cdc


I have a link on my site, but when the user clicks on it I don't want them to go anywhere but I want to do some JS processing. Right now this is the code I have:


<a href="#" id="myid" onclick="myFunction()">Click Here</a>


Is there something better I can put in the href field? Putting nothing in there seems to reload the page when the user clicks it (this code is on the homepage).

perkiset

Yup - that's a common issue with doing that. Here you go:

<a href="

javascript

 :" onClick="doSomethingElse()">anchor text</a>

You can actually put any

javascript

  in the href attribute - putting blank

javascript

  just causes the browser to do a delightful nothing.

/p

cdc

It's now bringing up Firefox's

JavaScript

  Error Console every time I click on the link. The console is empty, but it's still annoying as hell.

Here's the code I'm using (names changed to protect the guilty):

<a href="

javascript

 :" id="b" onclick="c()">a</a>


Am I missing something?

perkiset

Don't think so... holdon - firing up FF...

perkiset

... I'ts working perfectly for me... the only way that I get an error is if there's a problem with doSomething() or if I click on doSomething and doSomething() is not defined...


<html>
<body>

<a href="

javascript

 :" id="blowme" onclick="doSomething()">Testing!</a>

<script>
function doSomething()
{
        alert('here');
}
</script>
</html>

perkiset

... also works great in Safari and IE...

What exactly is the error in the FF debug panel?

cdc

That's the thing...there is no error in the console window. It just pops up empty...

Strangely enough, I have my JS printing out some HTML that has the same code, with

javascript

 : in the href and it works without issue.

perkiset

If it's just coming to the foreground, but is coming up empty then I believe that it's a JS thang in FF and not an error. If the JS is behaving the way you expect it and the debug console stays empty I think you're good to go. You might check and see if there are some properties or preferences somewhere that might be set weird...

cdc

Yes, everything is working fine in both firefox and IE, but this stupid window keeps popping up in FF. I'll test it on the wife's computer tomorrow.

Thanks for the help.

perkiset

I can't replicate for anything. It sounds as though FF decides to bring it forward ... but mine isn't doing it at all. Really strange. Looking forward to hearing what it was...

/p

cdc

Figured it out. I had to put

javascript

 :void(0); in the href field.

It was happening on my

mac

 hine as well as the wife's, so it's not some weird plugin that I have installed. My FF version is:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

I'm not sure what the wife's is, but she never uses it so it's probably a bit old.

joebloggs

quote author=perkiset link=topic=105.msg503#msg503 date=1177524792

Yup - that's a common issue with doing that. Here you go:

<a href="

javascript

 :" onClick="doSomethingElse()">anchor text</a>


I'm not a big fan of

javascript

  pseudo-protocols in the href attribute - that should always be a URL.
Instead, why not just cancel the default action of the link with return false? So instead just do:

<a href="aProperUrl.html" onclick="doSomethingElse(); return false">anchor text</a>

Or, even have the function returning a false value, so the above can be reduced further to:

<a href="aProperUrl.html" onclick="return doSomethingElse();">anchor text</a>

where

function doSomethingElse() {
  // do something else logic

  // and return a boolean response
  return false;
}

cdc

So the onClick code is executed first, and if it returns false the href is ignored?

perkiset

That is correct - a return false will halt the execution of the URL.

But this is my only argument against putting a link there - if a user (like me) pays attention to it, then it looks like the link will go somewhere rather than do something. So you could fool this by putting a completely farsical domain there to "tip the user" about what's going on, or use the JS so that they'll know they are not going anywhere.

Regarding void(0) - that'll work fine as well - you've got me concerned though because I've used the blank

javascript

  href with great success for a while and have never seen that occurrence... or at least have never been told that.

Methinks I need to review that method... thanks C

/p


Perkiset's Place Home   Politics @ Perkiset's