Problem with Javascript in <A>

J

Julius Mong

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=ca...adobe+svg+javascript+problem&hl=en&ie=UTF-8#5

4. Due to design limitations of Netscape, avoid using HTML with the
following form: <a href="#" onclick="myScriptWhichModifiesSVG()">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="javascript:myScriptWhichModifiesSVG()">

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="document.getElementById('svg0').setAttribute('viewBox','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="javascript:document.getElementById('svg0').setAttribute('viewBox
','56.8436 42.5733 308.911 143.705'); ">
<path d="bla" etc />
</a>

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

<g xmlns="http://www.w3.org/2000/svg"
onclick="javascript:document.getElementById('svg0').setAttribute('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
 
D

DU

Julius said:
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=ca...adobe+svg+javascript+problem&hl=en&ie=UTF-8#5

4. Due to design limitations of Netscape, avoid using HTML with the
following form: <a href="#" onclick="myScriptWhichModifiesSVG()">

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
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.

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
can be avoided by re-writing the code as follows: <a
href="javascript:myScriptWhichModifiesSVG()">

Use a real button for such call and code the onclick.
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="document.getElementById('svg0').setAttribute('viewBox','56.8436
42.5733 308.911 143.705'); ">
<path d="bla" etc />
</a>

An anchor should load another page, a resource somehow. Here, you should
be using an input button or an HTML button instead.
But if I did:

<a xmlns="http://www.w3.org/2000/svg"
xlink:href="javascript:document.getElementById('svg0').setAttribute('viewBox
','56.8436 42.5733 308.911 143.705'); ">
<path d="bla" etc />
</a>

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

<g xmlns="http://www.w3.org/2000/svg"
onclick="javascript:document.getElementById('svg0').setAttribute('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

http://jibbering.com/faq/#FAQ4_24

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.
"
http://www.useit.com/alertbox/20021223.html

This is particularly true for href="javascript: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="javascript:window.open(blah)">
* right:
<a href="foo.html" target="_blank" onClick="window.open(blah);
return false">
"
http://www.panix.com/~aahz/javascript.html#remove

DU
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top