Log in

View Full Version : href="?"



spyder
05-13-2007, 01:44 AM
i have a link which i want to change the visibility of a hidden table. it all works fine except i need to put something in the href="" of the link so the css works with it. i put a # which works, but it changes the url of the page. is there something that i could put in it that doesnt change anything?

thetestingsite
05-13-2007, 01:47 AM
Well, depending on how you change the visibility of the hidden table, you could probably use "return false;" in the onclick attribute like so:



<a href="#" onclick="theFunction(); return false;">Link</a>


Hope this helps.

djr33
05-13-2007, 02:51 AM
There are various ways to do this, and each is weird in some way.

<a href="javascript:doStuff();">
is one option, but would be weird for a browser without javacript and also looks weird to an inexperienced user on the status bar (they think they link goes to javascript:something). It might also make them think there is some weird security risk. Though it's the same as the more behind-the-scenes methods, the user would see it and perhaps be more worried.

<a href="#" onClick="...">
As you have, this does add the # to the url, but that's just a target. In some cases, you could make it work, like href="#pic1", if you're loading images. That might make it seem a bit more logical to the user.
I agree, though, that this is annoying.

Using return false is definitely the best option, as said above. (However, I prefer using "return [javascript]", which returns the JS, rather than the href, and is less to type, and I think might be more compatible.)
One thing to consider, though, is having a pure-html (non-javascript) backup. That's what I like about 'return':

With the example of a popup window:
<a href="popup.htm" onClick="return window.open(this.href);">

That would popup a window of the same value as the href of the tag, and do that instead (due to 'return') of going to the link. In a browser that doesn't have javascript, that's great, so they can still go to the link, rather than not being able to access the content. In fact, adding the target="_blank" attribute is nice, so you have a 'popup' window even with just html.


Anyway, that might not fit exactly what you are doing, but the idea of linking to a page as a substitute for users who can't access the javascript might be a good idea.

for example, if you had an image gallery:
<a href="image03.jpg" onClick="return changeImg(this.href);" target="_blank">

jscheuer1
05-13-2007, 03:32 AM
With the example of a popup window:
<a href="popup.htm" onClick="return window.open(this.href);">

With the above and similar, when javascript is enabled, it will execute both the new window and change the main page. The only way to be sure that it doesn't is to return false after opening the new window:


<a href="popup.htm" onClick="window.open(this.href);return false;">

If javascript is disabled, this will load the image in the current window. With javascript, it will leave the current window alone and attempt a pop up (subject to blocking, but allowed with most user's settings).

With your other exmple:


<a href="image03.jpg" onClick="return changeImg(this.href);" target="_blank">

This will execute the href, even with javascript enabled, unless the function changeImg itself returns false.

djr33
05-13-2007, 04:37 AM
How very strange. Nevermind, then. I was sure that worked. Maybe it's something overlooked in some browsers, so when I tried it, I thought it was right?

techno_race
06-21-2007, 02:48 AM
<a href="javascript:" onMouseOver="var statusbar="Change table visibility"; window.status=statusbar" onClick="TheFunction();">link</a>
Change the stuff in red to your own stuff.

tech_support
06-21-2007, 07:00 AM
1. Thanks for bumping up a thread that's already been answered A COUPLE OF MONTHS AGO. You're just trying to boost your post count.

2. The code's wrong.

<a href="javascript:" onMouseOver="var statusbar="Change table visibility"; window.status=statusbar" onClick="TheFunction();">link</a>
should be

<a href="#" onmouseover="var statusbar='Change table visibility'; window.status='statusbar';" onclick="TheFunction();">link</a>
3. This code only works in IE. FF does not let you to tamper with the status bar, and I think some other browsers to.
4. Don't try and hide everything to the user, it'll just annoy them.

techno_race
06-21-2007, 02:17 PM
1. We have absolutely no clue if he still needs help.
2. Yes, the code was wrong.
3. I didn't notice how old this thread was.
4. I don't even pay attention to my post count nowadays.
5. Yes, the code does only work in IE, but that's fine, considering that about 50&#37; of all people use it, including me.
6. What am I hiding from the user? I'm telling them it will change the table visibility, which it will. Do they expect the link to point to changeVisibility()?

Twey
06-21-2007, 05:10 PM
1. We have absolutely no clue if he still needs help.There were no further questions, so it's safe to assume so.
3. I didn't notice how old this thread was.How did you even stumble onto it?
5. Yes, the code does only work in IE, but that's fine, considering that about 50&#37; of all people use it, including me.That means it won't work for at least half your visitors. This isn't such a problem if there's non-JS fallback, as of course there should be, but you should at least have your code error out gracefully (try...catch is your friend). When it's simple to make it more compatible, however, there's really no excuse. None of this applies here, of course, where it will degrade gracefully anyway and isn't vital.
2. The code's wrong.So's yours: those quotes will cause the literal string 'statusbar' to be displayed.
<a href="#" onmouseover="status = 'Change table visibility';return false;" onmouseout="status = ''; return true;" onclick="TheFunction(); return false;">link</a>But, of course, to use an <a> element purely to fire Javascript is an abuse of the element anyway. Use a button, or a generic element such as <span>.
3. This code only works in IE. FF does not let you to tamper with the status bar, and I think some other browsers to.Not quite accurate. Most modern browsers disable status-bar shenanigans by default, but it's usually an option. Either way, it doesn't really matter: all browsers of which I know simply don't do anything when status bar modifications are disabled, so it degrades nicely anyway for this non-vital function.
4. Don't try and hide everything to the user, it'll just annoy them.I agree with techno_race on this one. So long as the provided message is an accurate description of what the link's meant to do, it will actually be more informative than a Javascript function name or a hash in the status bar.

techno_race
06-21-2007, 07:49 PM
How did you even stumble onto it?
I think I was browsing the HTML forum and forgot what page I was on.

tech_support
06-22-2007, 06:32 AM
That means it won't work for at least half your visitors. This isn't such a problem if there's non-JS fallback, as of course there should be, but you should at least have your code error out gracefully (try...catch is your friend). When it's simple to make it more compatible, however, there's really no excuse. None of this applies here, of course, where it will degrade gracefully anyway and isn't vital.

...and if you're trying to sell something, well... you won't go very well.


I agree with techno_race on this one. So long as the provided message is an accurate description of what the link's meant to do, it will actually be more informative than a Javascript function name or a hash in the status bar.
I like the links... Especially if I'm visiting a spam-could-be site.
Anyways, the "title" attribute would be better.

alexjewell
06-22-2007, 01:56 PM
Well, I can understand using the status bar change in this situation. But of course (like anything else), it can be abused. Doesn't mean we shouldn't use it properly and honestly though...and that's exactly what that code does.