简体   繁体   English

SVG错误:拒绝访问属性“ someFunction”的权限

[英]SVG Error: Permission denied to access property 'someFunction'

Please take a look at this fiddle: http://jsfiddle.net/arasbm/Tyxea/14/ 请看一下这个小提琴: http : //jsfiddle.net/arasbm/Tyxea/14/

As you can see I want to transform SVG elements when an event is triggered on them. 如您所见,当事件触发时,我想转换SVG元素。 You can click on arrow and it should work, because it uses the JavaScript code embeded inside the SVG scope: 您可以单击箭头,它应该起作用,因为它使用了嵌入SVG范围内的JavaScript代码:

<svg id="my-svg" width="20cm" height="20cm" viewBox="0 0 600 600"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <desc>Example showing how to transform svg elements 
        using SVGTransform objects</desc>
  <script type="application/ecmascript"> <![CDATA[
    function transformMe(evt) {
      // svg root element to access the createSVGTransform() function
      var svgroot = evt.target.parentNode;

      // SVGTransformList of the element that has been clicked on
      var tfmList = evt.target.transform.baseVal;

      // Create a seperate transform object for each transform
      var translate = svgroot.createSVGTransform();
      translate.setTranslate(50,5);

      var rotate = svgroot.createSVGTransform();
      rotate.setRotate(10,0,0);

      var scale = svgroot.createSVGTransform();
      scale.setScale(0.8,0.8);

      // apply the transformations by appending the SVGTranform objects 
      // to the SVGTransformList associated with the element
      tfmList.appendItem(translate);
      tfmList.appendItem(rotate);
      tfmList.appendItem(scale);
    }
  ]]> </script>

  <polygon fill="orange" stroke="black" 
    stroke-width="5" 
    points="100,225 100,115 130,115 70,15 70,15 10,115 40,115 40,225"
    onclick="transformMe(evt)"/>
  ...
</svg>

This works, but I would like to have my JavaScript code separate from SVG element. 这可行,但是我想将我的JavaScript代码与SVG元素分开。 According to this W3C document I should be able to have call a javascript function by refering to it using top scope. 根据此W3C文档,我应该能够通过使用top范围引用JavaScript函数来调用它。 That is what I have done for the rectangle: 那就是我对矩形所做的:

  <rect x="200" y="100" width="100" height="100" 
    fill="yellow" stroke="black" stroke-width="5"  
    onclick="top.transformMe(evt)"/>

However clicking on the rectangle gives the following error in console: 但是,单击矩形会在控制台中出现以下错误:

  Error: Permission denied to access property 'transformMe' @ http://fiddle.jshell.net/arasbm/Tyxea/14/show/:1

Can someone tell me how to solve this problem. 有人可以告诉我如何解决这个问题。 The real question I have that is demonstrated in this example is: what is the proper way to handle events on SVG elements with JavaScript code that is outside those elements? 我在这个示例中演示的真正问题是: 用那些元素之外的JavaScript代码处理SVG元素上的事件的正确方法什么?

The problem in the fiddle code is the way JSFiddle arranges your code. 小提琴代码中的问题是JSFiddle安排代码的方式。

First, the Javascript is evaluated in a function body and thus your method transformMe does not become a global function. 首先,在函数体内评估Javascript,因此您的方法transformMe不会成为全局函数。 Add

window.transformMe = transformMe 

at the end of your Javascript so that the function becomes a global. 在Javascript的末尾,以便该函数成为全局函数。

Then again in the fiddle the code runs in an iframe (maybe your page is different) and "top" refers to the top document and in the case of jsfiddle.net you are thus trying to make a cross-domain JS call. 然后,再次在小提琴中,代码在iframe中运行(也许您的页面有所不同),并且“ top”是指顶级文档,对于jsfiddle.net,您正尝试进行跨域JS调用。 You can see this if you run the fiddle with the developer tools turned on: the console gives the right hints. 如果您在打开开发人员工具的情况下运行小提琴,便会看到此信息:控制台会提供正确的提示。

Last but not least in the current browser implementations I don't believe you need the "top" reference at all. 最后但并非最不重要的一点是,在当前的浏览器实现中,我认为您根本不需要“ top”引用。 Instead you can simply call global functions (just tested it with IE, Chrome, and FF and it worked for me ). 相反,您可以简单地调用全局函数(只需在IE,Chrome和FF上对其进行测试就可以了 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM