Problem with Javascript in <A>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julius Mong

    #1

    Problem with Javascript in <A>

    Dear all, according to the ASV3 manual known problem section, to make an <a>
    execute some Javascript onclick:



    4. Due to design limitations of Netscape, avoid using HTML with the
    following form: <a href="#" onclick="myScri ptWhichModifies SVG()">The problem
    is that JavaScript responds to the onclick event and begins executing the
    script, but then Netscape also responds to the fact that you clicked on a
    link, and interrupts JavaScript to re-load the current page. This conflict
    can be avoided by re-writing the code as follows: <a
    href="javascrip t:myScriptWhich ModifiesSVG()">

    Now that supposedly only occurs in Netscape, but I'm using IE6 + ASV6 and I
    am experiencing the exact same problem:

    <a xmlns="http://www.w3.org/2000/svg" xlink:href="#"
    onclick="docume nt.getElementBy Id('svg0').setA ttribute('viewB ox','56.8436
    42.5733 308.911 143.705'); ">
    <path d="bla" etc />
    </a>

    But if I did:

    <a xmlns="http://www.w3.org/2000/svg"
    xlink:href="jav ascript:documen t.getElementByI d('svg0').setAt tribute('viewBo x
    ','56.8436 42.5733 308.911 143.705'); ">
    <path d="bla" etc />
    </a>

    This will cause IE to throw an error saying document.getEle mentById()
    returns null, but if I replaced <A> with <G> like below:

    <g xmlns="http://www.w3.org/2000/svg"
    onclick="javasc ript:document.g etElementById(' svg0').setAttri bute('viewBox', '
    56.8436 42.5733 308.911 143.705'); ">
    <path d="bla" etc />
    </g>

    It works perfectly fine, and so I have no idea if it is at all possible to
    run Javascript in href in SVG anchors. Please help.

    Thanks, Julius


  • DU

    #2
    Re: Problem with Javascript in &lt;A&gt;

    Julius Mong wrote:[color=blue]
    > Dear all, according to the ASV3 manual known problem section, to make an <a>
    > execute some Javascript onclick:
    >
    > http://www.google.co.uk/search?q=cac...=en&ie=UTF-8#5
    >
    > 4. Due to design limitations of Netscape, avoid using HTML with the
    > following form: <a href="#" onclick="myScri ptWhichModifies SVG()">[/color]

    You should use a real, genuine button to make such js call. I would not
    use a phony-balloney anchor to modify SVG.

    The problem[color=blue]
    > is that JavaScript responds to the onclick event and begins executing the
    > script, but then Netscape also responds to the fact that you clicked on a
    > link, and interrupts JavaScript to re-load the current page.[/color]

    MSIE 6 (or Opera 7.x or any other browser) would do the same too because
    you do not cancel the default action of the HTML element, which is to
    load in the same page the href attribute value. href="#" usually brings
    the page back to the top.

    This conflict[color=blue]
    > can be avoided by re-writing the code as follows: <a
    > href="javascrip t:myScriptWhich ModifiesSVG()">
    >[/color]

    Use a real button for such call and code the onclick.
    [color=blue]
    > Now that supposedly only occurs in Netscape, but I'm using IE6 + ASV6 and I
    > am experiencing the exact same problem:
    >
    > <a xmlns="http://www.w3.org/2000/svg" xlink:href="#"
    > onclick="docume nt.getElementBy Id('svg0').setA ttribute('viewB ox','56.8436
    > 42.5733 308.911 143.705'); ">
    > <path d="bla" etc />
    > </a>
    >[/color]

    An anchor should load another page, a resource somehow. Here, you should
    be using an input button or an HTML button instead.
    [color=blue]
    > But if I did:
    >
    > <a xmlns="http://www.w3.org/2000/svg"
    > xlink:href="jav ascript:documen t.getElementByI d('svg0').setAt tribute('viewBo x
    > ','56.8436 42.5733 308.911 143.705'); ">
    > <path d="bla" etc />
    > </a>
    >
    > This will cause IE to throw an error saying document.getEle mentById()
    > returns null, but if I replaced <A> with <G> like below:
    >
    > <g xmlns="http://www.w3.org/2000/svg"
    > onclick="javasc ript:document.g etElementById(' svg0').setAttri bute('viewBox', '
    > 56.8436 42.5733 308.911 143.705'); ">
    > <path d="bla" etc />
    > </g>
    >
    > It works perfectly fine, and so I have no idea if it is at all possible to
    > run Javascript in href in SVG anchors. Please help.
    >
    > Thanks, Julius
    >
    >[/color]



    Top Ten Web-Design Mistakes of 2002
    6. JavaScript in Links
    "(...) Links that don't behave as expected undermine users'
    understanding of their own system. A link should be a simple hypertext
    reference that replaces the current page with new content. Users hate
    unwarranted pop-up windows. When they want the destination to appear in
    a new page, they can use their browser's "open in new window" command --
    assuming, of course, that the link is not a piece of code that
    interferes with the browser’s standard behavior.
    "
    Every year brings new mistakes. In 2002, several of the worst mistakes in Web design related to poor email integration. The number one mistake, however, was lack of pricing information, followed by overly literal search engines.


    This is particularly true for href="javascrip t:window.open(. ..);" links:
    even browsers (like NS 7.x, Mozilla) get confused and get fooled when
    examining the properties of such links. Browsers will indicate that the
    link will open the referenced resource in the same window while this is
    not true!

    "
    * Don't use javascript: URLs
    Using a straight http: URL will allow any browser to access the
    link. If you want to use JavaScript for browsers that have JavaScript
    enabled, use the onMouseOver and onClick attributes of the <a href> tag.
    (...)

    Here's a simple example that demonstrates how one can still use
    JavaScript while making the site accessible to people with JavaScript
    turned off:

    * wrong:
    <a href="javascrip t:window.open(b lah)">
    * right:
    <a href="foo.html" target="_blank" onClick="window .open(blah);
    return false">
    "


    DU

    Comment

    Working...