469,609 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Problem with Javascript in <A>

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="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').setAttrib ute('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('sv g0').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
Jul 23 '05 #1
1 1825
DU
Julius Mong wrote:
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="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').setAttrib ute('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('sv g0').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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Donald Firesmith | last post: by
4 posts views Thread by Guillaume CABANAC | last post: by
3 posts views Thread by ajay2552 | last post: by
2 posts views Thread by optimistx | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.