SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why you should never use "javascript:" in a link

    (Hopefully) useful tip for anyone who uses javascript. Don't use "javascript:" in a link URL to fire a javascript function. There is absolutely no reason to use it (apart from making bookmarklets but that's something else entirely). Browsers that don't support javascript will choke on it when for no extra cost you could use it to at least give them some kind of warning message.

    Basically, instead of this:
    Code:
    <a href="javascript:doSomething()">click</a>
    Do this:
    Code:
    <a href="noJS.html" onClick="doSomething(); return false;">click</a>
    Set noJS.html up as a simple page that informs the user that javascript must be turned on for that feature to work. Browsers that understand javascript will fire the function from the onClick event and then NOT visit the link due to the return false; command.

    Even better, if clicking the link is meant to make a popup window appear use this:
    Code:
    <a href="popup.html" target="_blank" onClick="popUpFunction('popup.html', 100, 100); return false;">click</a>
    That way browsers without javascript will still open the pop up page in a new window.

    I think the javascript: protocol thingy is deprecated now as well, but I can't find a link to back that up.

    Cheers,

    Skunk

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No - the "javascript:" thingy is NOT deprecitated!
    It's a basic part of syntax.

    But everything else is sound ( not that i care about people that don't use JS on my sites )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's deprecated according to this: http://www.scottandrew.com/weblog/000284

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the first reference i've ever seen saying it is.
    And i'm afraid i can't take that opinion at face value.
    The syntax isn't depreciated, as far as i'm aware, since there is no replacement for it!
    onclick doesn't do the same thing (don't get pedantic - we're obviously not talking about effects here , and there are places where you might specifically want to use javascript for a href - most of these have to do with fairly exotic techniques of how the call is handled - and i can't think of any right now otherwise i'd give you an example.
    I agree it shouldn't be used if it can be avoided - but neither that fact, nor the opinion of some guy somewhere, makes it depreciated - that's down to W3C!!! ( who as far as i know have definately NOT depreciated the term )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    . Ruchir's Avatar
    Join Date
    Feb 2002
    Location
    Sydney
    Posts
    1,863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do not use "javascript:"? Well thats very strange cause a lot of people use it and it aint a prob most of the time ... avoiding it is ok. but i dont think its wrong to use it .. it has to be accepted cause it is valid as a value that determines a keyword to be of the javascript.

  6. #6
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's only a problem on browsers with javascript disabled. I know lots of people use it, my point is that they shouldn't because onClick provides the same functionality but allows you to use the href part to at least provide something to browsers without javascript - hence there's no reason at all to sue javascript: instead of onclick (there are no disadvantages to the onclick method but there are disadvantages to the javascript: method).

  7. #7
    SitePoint Addict ThomasAesir's Avatar
    Join Date
    May 2002
    Location
    Sydney, Australia
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    redirect

    It's only a problem on browsers with javascript disabled.
    You can always redirect all browsers that have JavaScript disabled, using a noscript tag, to a page that tells them to use a javascipt enabled Web browser.

    How can anyone surf the net without javascript anyway? They must have problems using 80-90% of all Websites.
    Thomas Oeser - Blueprint Software
    Web Scripting Editor v 5.2 One cool Web editing tool.
    3dcomputergraphics.com Coming Soon!

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: redirect

    Originally posted by ThomasAesir
    How can anyone surf the net without javascript anyway? They must have problems using 80-90% of all Websites.
    I do, but then maybe I'm just freaky. Personally I never use javascript on any of my sites for the simple reason people like me might not have it enabled and also because just about everything can be crafted with no extra effort to be done without it.
    Oh no! the coots are eating my nodes!

  9. #9
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The problem isn't just for people who have javascript disabled

    Using href="javascript:" causes a problem whenever a user right clicks on the link and then selects "Open in New Window." Often, I see pages that opena link in a new window using javascript: and when I manually choose "Open In New Window," the link breaks. That's why the author's approach (using onClick) is a good one.

  10. #10
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I frickin hate sites that use javascript for opening links. It disables my beloved "open in new window", which really, really sucks.

    Also, such scripting makes it toally impossible to browse the site from a PDA.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  11. #11
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i totally agree with Skunk and Mattias... i often "open a new window" too and i hate it when these sites try to tell me how i want to manage my life!

    it's also not gonna work in text browsers (ok that's pushing it a bit but there are people who use lynx... check the stats page of any respectable website)

  12. #12
    SitePoint Guru nagrom's Avatar
    Join Date
    Jul 2001
    Location
    Western CT, USA
    Posts
    803
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a *great* article on this subject:
    http://www.evolt.org/article/rating/17/20938/index.html

  13. #13
    eigo hanasemasu ka? Yes. =) ZuulJin's Avatar
    Join Date
    Dec 2001
    Location
    Japan
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I say f*** the people without Javascript enabled. If your so damn paranoid that javascript scares you...

    jeez!

    Although I agree that "javascript:" doesn't need to be used. And I am more a PHP programmer than Javascript.

    and whoever makes those PDA web browsers need to find a way to deal with Javascript... come on. Javascript is a PART of the web. Nothing can be done about that.
    U.S. DoD Member in Japan?
    Choose your base. Buy|Sell. Easy
    @ APO Ads.



  14. #14
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    most of them (i do enable JavaScript) aren't paranoid... i believe they're more bothered by popups and all that annoying things certain sites do... like adding themselves as a bookmark without prompting...

    and i really hate it when a site breaks down because of some JavaScript error...
    Last edited by redemption; Jul 2, 2002 at 22:25.

  15. #15
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ZuulJin
    and whoever makes those PDA web browsers need to find a way to deal with Javascript... come on. Javascript is a PART of the web. Nothing can be done about that.
    No not Javascript, but they could at least allow a few more features - or in other words: there are other things they could implement first. You should remember that most PDA's require different "versions" of your website than a normal browser.

    I don't know about other PDA's, but the Palm ones use HTML 3.2 with a few limitations imposed by Palm, such as:
    No frames, image maps or tricky text
    No colour tags, anywhere
    Limit tables to 140x150 and use them both sparingly and elegantly
    No font tags
    Bold face is <strong></strong> (XHTML)
    Text size MIGHT respond to <big></big> and <small></small> tags
    Use high contrast, very small images - with alt tags
    Rely on word-wrapping to position your text correctly - that is, dont' put text into tables
    Plan forms for both wireless and disconnected action

    I'm not sure how relevant these limitations are to the current models and other platforms, but they were valid in May for Palm's.
    Oh no! the coots are eating my nodes!

  16. #16
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ZuulJin
    and whoever makes those PDA web browsers need to find a way to deal with Javascript... come on. Javascript is a PART of the web. Nothing can be done about that.
    Well I agree... kind of. Problem is that PDA:s are not meant to be albe to open new windows and stuff, which javacript is mostly used for. It's just not possible to have that kind of window managment on a such a small screen, no matter how good the resolution is.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  17. #17
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that is the point really, you have to remember that the PDA was developed as, and still is, a personal organiser. The possibilities of the synch cradle thurst the Internet upon it and the development of wireless gave it wings. But it has a way to go yet before it soars.
    Oh no! the coots are eating my nodes!

  18. #18
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I use my iPaq H3870 to browse the internet completely wirelessly at 56k modem-speeds, with quite good results, actualy. I'd say that technology is ready - I can quickly check up if a domain is free while talking to a client, for instance. Amazon.com has a nice special version of their site for Pocket PC:s. I CAN read ordinary web sites in Pocket IE, but customized solutions are much sleeker.

    Believe me when I say this - solutions customized for PDA:s are going to be a big oppurtunity.
    Last edited by M. Johansson; Jul 3, 2002 at 03:16.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  19. #19
    SitePoint Zealot Mr Chocolate's Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    View your source right now on this page.. What do you see? good old javascript!

  20. #20
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mr Chocolate
    View your source right now on this page.. What do you see? good old javascript!
    Your point being?
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  21. #21
    SitePoint Zealot Mr Chocolate's Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like using Javascript!


  22. #22
    SitePoint Guru nagrom's Avatar
    Join Date
    Jul 2001
    Location
    Western CT, USA
    Posts
    803
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ZuulJin
    I say f*** the people without Javascript enabled. If your so damn paranoid that javascript scares you...
    what about people that browse from work, where many IT departments disable javascript? or at libraries and colleges?

    there's no reason (except laziness) that a site can't work great with javascript, and function without it. there are also legal, accessibility issues to consider, but those differ from company to company and country to country.

    as a developer though, you should be aware that depending entirely on javascript could get you into trouble.

  23. #23
    SitePoint Zealot Mr Chocolate's Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very true. I think it all depends on the site your working on if it's your own site then i say use all the javascript you want!. But if your making a site for a client you should make your site work in as many browsers as possible! But i think every body here knows that.

  24. #24
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mr Chocolate
    But if your making a site for a client you should make your site work in as many browsers as possible! But i think every body here knows that.
    Alternatively in ALL browsers Except lynx cos it is just screwed up!
    Oh no! the coots are eating my nodes!

  25. #25
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you design by using properly structured HTML (or XHTML) and keep all of your layout code in CSS there's no reason you wouldn't be able to get a page that works well in lynx.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •