void(0) is disabling my onclick event (ie only) ???

Discussion in 'Javascript' started by seth.m.green@gmail.com, Dec 21, 2005.

  1. Guest

    <a href="javascript: void(0);"
    onclick="window.location.href='/foo.bar?one=false&two=true'">Link</a>

    works like a charm in Firefox. I get NOTHING in IE. but if I replace
    the javascript: void(0) with just a #, then it works in IE. wtf? Any
    help.
    , Dec 21, 2005
    #1
    1. Advertising

  2. McKirahan Guest

    <> wrote in message
    news:...
    > <a href="javascript: void(0);"
    > onclick="window.location.href='/foo.bar?one=false&two=true'">Link</a>
    >
    > works like a charm in Firefox. I get NOTHING in IE. but if I replace
    > the javascript: void(0) with just a #, then it works in IE. wtf? Any
    > help.
    >


    Isn't what you're doing the same as the following?

    <a href="/foo.bar?one=false&two=true">Link</a>


    Or maybe these links will help:

    "So, when you want to place an onclick handler on an anchor tag, ..."
    http://www.themaninblue.com/writing/perspective/2004/08/05/

    Anchors and Links
    "If you supply a value for the ONCLICK attribute,
    the specified action overrides the default link behavior."
    http://devedge-temp.mozilla.org/library/manuals/1998/htmlguide/tags7.html

    Javascript Best Practices
    Using onClick in <A> tags
    http://www.mattkruse.com/javascript/bestpractices/
    McKirahan, Dec 21, 2005
    #2
    1. Advertising

  3. Guest

    yes, it is the same as just putting the url in the href attribute, but
    I don;t want bots to follow the link, so I am having javascript perform
    the redirect. I appreciate the links you mention but none of them
    explain the behavior I am seeing. I'm not so much interested in other
    techniques, instead I am interested in understanding why the code I
    have written doesn't function in IE.
    , Dec 21, 2005
    #3
  4. wrote:

    > yes, it is the same as just putting the url in the href attribute, but
    > I don;t want bots to follow the link, so I am having javascript perform
    > the redirect.


    Not a great idea ...

    1 - Not every member of the group of user agents "not bots" supports
    JavaScript

    2 - Not every member of the group of user agents "not bots that support
    JavaScript" has JavaScript turned on

    and the biggy ...

    3 - Not every member of the group of user agents "bots" does not support
    JavaScript.

    I've got a nice little bot (Pavuk) which supports JavaScript.

    Just use robots.txt to ask bots to keep away from those URLs.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Dec 21, 2005
    #4
  5. VK Guest

    wrote:
    > <a href="javascript: void(0);"
    > onclick="window.location.href='/foo.bar?one=false&two=true'">Link</a>
    >
    > works like a charm in Firefox. I get NOTHING in IE. but if I replace
    > the javascript: void(0) with just a #, then it works in IE. wtf? Any
    > help.


    There is no technical reasons for that. Must be some security patch or
    a side effect of some security patch. Use instead:

    <a href="javascript:void(0)"

    onclick="setTimeout('window.location.href=\'/foo.bar?one=false&two=true\';')">Link</a>
    VK, Dec 21, 2005
    #5
  6. On 21/12/2005 22:39, VK wrote:

    [snip]

    > <a href="javascript:void(0)" [...]


    Just how many times must you be asked not to recommend rubbish like that?

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Dec 21, 2005
    #6
  7. RobG Guest

    wrote:
    > <a href="javascript: void(0);"
    > onclick="window.location.href='/foo.bar?one=false&two=true'">Link</a>
    >
    > works like a charm in Firefox. I get NOTHING in IE. but if I replace
    > the javascript: void(0) with just a #, then it works in IE. wtf? Any
    > help.
    >



    This from a recent Richard Cornford post seems appropriate:

    "IE treats the activation of a javascript pseudo-protocol HREF as
    navigation, and when used the browser goes into a 'waiting state'
    pending the arrival of a replacement for the content. In this
    state many previously available browser facilities are withdrawn,
    including GIF animation and image swapping.

    "The normal strategy for dealing with this issue is to never use a
    javascript pseudo-protocol HREF that will not result in the
    replacement of the contents of the current page (so never to execute
    a function for its side effect).

    "Usually the same effect as a javascript pseudo-protocol HREF can be
    achieved (without the detrimental consequences in IE) with a suitably
    default action cancelling intrinsic event handler. An onclick
    handlers are the usual replacement."

    <URL:
    http://groups.google.com/group/comp...ity, img elements dissappear#ed05d2132efcf609
    >


    So the solution is:

    <a href="whyLinkDidntWork.html"
    onclick="window.location.href='/foo.bar?one=false&two=true'">Link</a>

    Bots (if they're not smart enough to follow the onclick) will get the
    same text page that users sans JS get.


    --
    Rob
    RobG, Dec 22, 2005
    #7
  8. Guest

    Yes, I understand all of that, and while I also understand your
    compulsion to teach best practices I know exactly what I am getting
    myself into. My #1 solution to my problem would have been to use a
    robots.txt file but you can scratch that because

    1. Not every bot adheres to robots.txt rules

    I have a very special case in which I need to do this. And while I too
    have been accused of "preaching" standards, and while I was practiciing
    "unobtrusive javascript" a long time ago, this is what I want to do
    know. I'm a big boy.

    Even if I didn't want to put this into practice, I would have asked the
    same question for academic reasons. It seems that no one can explain
    this behavior other than speculated it is a bug in some security patch
    (or something of that ilk).
    , Dec 22, 2005
    #8
  9. Guest

    interesting. thanks for the working code.
    , Dec 22, 2005
    #9
  10. Randy Webb Guest

    Randy Webb, Dec 22, 2005
    #10
  11. VK Guest

    Michael Winter wrote:
    > On 21/12/2005 22:39, VK wrote:


    > > <a href="javascript:void(0)" [...]

    >
    > Just how many times must you be asked not to recommend rubbish like that?


    ? It is not *my* code, it's copy'n'paste fragment of the OP's code
    where I changed a bit the part you did not quote. So you may address
    your claims to OP but he already comment on it rather clearly in his
    2nd post (see the thread). You may try once over again though - with
    *him*.

    P.S. On your leisure time google for keyword "bookmarklet". Also may
    try "bookmarklet site:mozilla.org"
    Then the buttle is lost - it's lost. That never was *my buttle* but my
    sincere condolescence anyway.
    VK, Dec 22, 2005
    #11
  12. Guest

    My simple theory:
    OnClick the window.location changes to "/foo.bar?one=false&two=true"
    Then milliseconds later, way before the connection could be made, it
    changes again, to "javascript: void(0);"

    First,
    onClick="window.location.href='/foo.bar?one=false&two=true';return
    false"
    This prevents default action from being performed (supposedly).
    Then, href="#" instead of href="javascript:void(0)". A link that moves
    you to current location within page, essentially doing nothing, not
    even reloading the page. Sideeffect: # gets appended to URL. Not a big
    deal. Three. I often use Lynx. I hate you.
    , Dec 22, 2005
    #12
  13. VK Guest

    wrote:
    > It seems that no one can explain
    > this behavior other than speculated it is a bug in some security patch
    > (or something of that ilk).


    IE is a closed source browser so whatever is not documented is forcely
    a subject of speculations. Here's another one: it may be a navigation
    request priority issue.
    "onclick" expression processed before href havigation. So in case like:
    <a href="go here" onclick="go there">
    there are two navigation requests pending. IE gives the priority to the
    "official" request from href (even if it doesn't lead anywhere). FF
    gives the priority to whoever asked first (onclick) - or it's so smart
    that it looks at href and sees that there is not any actual navigation
    in href. The latter could be checked by studying Firefox source code,
    the first will remain a speculation.
    VK, Dec 22, 2005
    #13
  14. Lee Guest

    said:

    >Even if I didn't want to put this into practice, I would have asked the
    >same question for academic reasons. It seems that no one can explain
    >this behavior other than speculated it is a bug in some security patch
    >(or something of that ilk).


    There's no bug involved.

    You've given the browser contradictory instructions. You've got
    an onclick handler that changes location but doesn't return false.
    That's a coding mistake.

    If you write code that says, in effect, "do something unpredictable",
    you shouldn't be surprised to find that you can't predict how it
    will behave.
    Lee, Dec 22, 2005
    #14
  15. On 22/12/2005 08:53, VK wrote:

    > Michael Winter wrote:
    >
    >> On 21/12/2005 22:39, VK wrote:
    >>
    >>> <a href="javascript:void(0)" [...]

    >>
    >> Just how many times must you be asked not to recommend rubbish like
    >> that?

    >
    > ? It is not *my* code [...]


    The moment you post a suggestion for someone to use, that /is/ your code
    and you are responsible for it. If you think that something is a bad
    idea, then don't recommend it.

    An alternate approach that may be workable for the OP is to submit a
    form, styling the submit button like a link (if necessary).

    [snip]

    > P.S. On your leisure time google for keyword "bookmarklet". Also may
    > try "bookmarklet site:mozilla.org"


    What on Earth do bookmarklets have to do with using the javascript
    pseudo-scheme in links?

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Dec 22, 2005
    #15
  16. VK Guest

    Michael Winter wrote:
    > On 22/12/2005 08:53, VK wrote:
    >
    > > Michael Winter wrote:

    > The moment you post a suggestion for someone to use, that /is/ your code
    > and you are responsible for it.


    One becomes responsible for someone code as soon as she quoted it?
    That's a newsgroup rule I was not aware of. Any link to read more?
    ;-)

    > What on Earth do bookmarklets have to do with using the javascript
    > pseudo-scheme in links?


    Not "bookmarks" - *bookmarklets*
    This is a very popular and fast growing scripting technics totally
    ignored so far in clj, but fully endorsed by all browser producers
    including Firefox. Their core functionality based on <a
    href="javascript:code"> usage. Just google a bit.

    As a consolation I can tell that you're winning the buttle over
    "<script type" instead of "<script language".

    50/50 result is not bad at all.
    VK, Dec 22, 2005
    #16
  17. On 22/12/2005 12:45, VK wrote:

    [snip]

    > One becomes responsible for someone code as soon as she quoted it?


    You didn't quote the code, you supplied it as a 'solution'. That it was
    mainly the OP's code is immaterial: you still posted it as /your/ advice.

    [snip]

    >> What on Earth do bookmarklets have to do with using the javascript
    >> pseudo-scheme in links?

    >
    > Not "bookmarks" - *bookmarklets*


    You really should learn to read, VK. Bookmarklets use the javascript
    pseudo-scheme. Links should not.

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Dec 22, 2005
    #17
  18. VK Guest

    Michael Winter wrote:
    > You really should learn to read, VK. Bookmarklets use the javascript
    > pseudo-scheme. Links should not.


    How do think bookmarlets are being displayed on the page? :-0
    <a href="javascript:code">Bookmark this util</a>

    Unless we're going to introduce a refined mental distinction between:
    <a href="javascript:code">Read RSS feed</a>
    // bad use
    and
    <a href="javascript:code">Bookmark to read RSS feed</a>
    // good use

    And what if someone used first <a href="javascript:code">Read RSS
    feed</a> to read the feed and later added it to bookmarks? Was it bad
    use which became good one at the moment of transfering to her bookmark
    collection?
    As I said: if the buttle is over - it's over.
    VK, Dec 22, 2005
    #18
  19. VK Guest

    VK wrote:
    > How do think bookmarlets are being displayed on the page? :-0
    > <a href="javascript:code">Bookmark this util</a>


    To stay completely within the *link* issue let's talk about so popular
    "ever fresh" links:

    <a href="javascript:
    var d = new Date();
    var u = ''+d.getFullYear()+d.getMonth()+d.getDay();
    window.location.href = 'http://www.myserver.com/'
    + u + '.html';
    ">Bookmark my webcam</a>
    VK, Dec 22, 2005
    #19
  20. Lee Guest

    VK said:

    >Unless we're going to introduce a refined mental distinction between:
    > <a href="javascript:code">Read RSS feed</a>
    > // bad use
    >and
    > <a href="javascript:code">Bookmark to read RSS feed</a>
    > // good use



    The use of the javascript: pseudo-protocol for its side-effect
    should be avoided when there is a better alternative.
    In the case of bookmarklets, there is no better alternative.
    That's the distinction.
    Lee, Dec 22, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Groeger
    Replies:
    2
    Views:
    496
    Michael Groeger
    Jan 20, 2006
  2. Ollej Reemt
    Replies:
    7
    Views:
    518
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    786
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    824
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    403
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page