Bug 55493 - SVG Tiny 1.2 onclick handler doesn't fire function defined in onload handler
Summary: SVG Tiny 1.2 onclick handler doesn't fire function defined in onload handler
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 525.x (Safari 3.2)
Hardware: Mac (Intel) OS X 10.6
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-01 12:16 PST by Tyler Pham
Modified: 2011-03-10 09:59 PST (History)
2 users (show)

See Also:


Attachments
illustration of the SVG onclick handlers (2.04 KB, application/xhtml+xml)
2011-03-01 12:16 PST, Tyler Pham
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tyler Pham 2011-03-01 12:16:07 PST
Created attachment 84270 [details]
illustration of the SVG onclick handlers

If a function is defined inside the onload handler of an SVG document, and trying to call it with an onclick from an SVG element will execute an error in in Safari 5.0.3(6533.19.4)

The red rectangle is set to handle "onclick" with a function call inside the onload handler:

	function red() {
		alert("red");
	}
	
The black rectangle is set to handle "onclick" with a function call outside the onload handler:

	function custom(evt) {
		alert(evt.target.id);
	}
	
Note: The error in Safari developer (Inspect Element) panel issues: "ReferenceError: Can't find variable: red" (red is the function)?
Comment 1 Alexey Proskuryakov 2011-03-01 23:11:50 PST
That's correct behavior - function red is not defined in global scope, it's defined in the scope of init function. You can use something like the below to define a function in global scope from init:

window.red = function() { alert("red") }
Comment 2 Tyler Pham 2011-03-10 09:39:05 PST
I understand that the resolution is to define the function globally.

I guess, I should rephrase my question:
Why is it that I cannot reuse a function declared locally?

This is no big issue to me but it is out of curiosity. For example using local variables ONLY within the scope of the init function.
Comment 3 Tyler Pham 2011-03-10 09:53:47 PST
I guess, this is a very primitive question. So I'll leave this "RESOLVED".

Thanks for your response, Alexey.
Comment 4 Alexey Proskuryakov 2011-03-10 09:59:22 PST
You can use the function (I actually suggested how to do that in comment 1). It's just not accessible by name from outside its scope. That's a good thing, because it lets one add functions in local scope without worrying about name clashes with functions defined in libraries like jQuery - similarly to local variables.