Jump to content

What's the point of href="#"


niche

Recommended Posts

What's the point of the # in - <a href="#" onclick="java script:showlayer('myName')">Show/Hide Layer</a>?Why just the #?

Link to comment
Share on other sites

It's just put in there to fill the href attribute, because the link triggers a JavaScript action. The tag should probably look more like below though:

<a href="#" onclick="return showlayer('myName')">Show/Hide Layer</a>

If the function returns false, the click event won't trigger the anchors usual action.

Link to comment
Share on other sites

How did you know to put the word "return" in the tag?"javascript" worked fine, but I don't know where the guidance came from to put it there.Thanks

Link to comment
Share on other sites

That means that the result of the function call will be passed to the actual link, and if false it will prevent the event from triggering anything else.

Link to comment
Share on other sites

Some more clarification please. I tried substituting "return" for "javascript" and the script stopped working. I can accept that "javascript" follows "onclick =" in this situation, but isn't there some documentation that says when and how that works (ie I'm just guessing that "javascript", in this case, is triggering the script in the style tag, but again I'm just guessing.How did the writer of the script know to put "javascript" after "onclick="? Thanks

Link to comment
Share on other sites

what does your javascript code look like?What triggers the javascript is the calling of the function, showlayer. I think what Synook exampled is what you'll find more commonly used.

Link to comment
Share on other sites

How did the writer of the script know to put "javascript" after "onclick="?
Like Synook said in another post:
It shouldn't [appear there]. The java script: bit refers to the JavaScript pseudo-protocol, which allows you to run JavaScript as a URL. However, you only need it if you write in the href attribute.
If you just remove the 'javascript' alltogether the script will still work. (ie. onclick="showlayer('my_name');")
Link to comment
Share on other sites

Like Synook said in another post:If you just remove the 'javascript' alltogether the script will still work. (ie. onclick="showlayer('my_name');")
haha, I literally just came back to post that. So I'll do it anyway. :)
It shouldn't. The java script: bit refers to the JavaScript pseudo-protocol, which allows you to run JavaScript as a URL. However, you only need it if you write in the href attribute.
Link to comment
Share on other sites

Now, that makes sense. I took the "javascript" out after "onclick" and the script still works (but then you knew that)!Thanks to Synook, zeekclub, ShadowMage, and thescientist for their help.I'll need to find time to at least familiarize myself with JavaScript pseudo-protocol.Thanks Again,Niche

Link to comment
Share on other sites

Now, that makes sense. I took the "javascript" out after "onclick" and the script still works (but then you knew that)!Thanks to Synook, zeekclub, ShadowMage, and thescientist for their help.I'll need to find time to at least familiarize myself with JavaScript pseudo-protocol.Thanks Again,Niche
maybe, although I would just stick by the example syntax provided, i.e. onclick="myFunction()", as you should note the specific reason why Synook said the pseudo class is used in the first place.
Link to comment
Share on other sites

Just to be clear on what 'return' does:Each element has a default action for certain events. I'll use the onclick event of an anchor as an example.The default action of an anchor tag is to load whatever is in the href attribute. If it's an anchor link (a hash link to a named anchor ie. href='#anchor') it will jump the document to that anchor. If it's a URL it will load that page.When you have a handler set, that handler will return a value to the element. If the value is true the element will perform it's default action, if false it will not perform it's default action.When you declare an event handler like this: onclick='function_name();' it returns true (Or maybe nothing at all, not sure) and executes the anchors default action of loading the href. So let's define the function:

function_name() {   alert("this is a function");   return false;}

Even though a return is specified in the function, it will still execute the anchor's default action when you call the function as above. To cancel the default action you must add the 'return' keyword to the call: onclick='return function_name();'This returns the value of the function to the element. Since the function returns false, the onclick handler will return false to the anchor, canceling its default action.To the experts: If I've missed anything or have anything incorrect, please correct me. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...