简体   繁体   English

在xmlhttprequest响应中使用svg web

[英]Using svg web in a xmlhttprequest response

I'm trying to load a svg image generated by a php file. 我正在尝试加载由php文件生成的svg图像。 If I use the standard svg notation it works for the browser that support svg, this is nothing new: 如果我使用标准的svg表示法,它适用于支持svg的浏览器,那么这并不是什么新鲜事:

    <svg><rect x="20" y="40" width="100" height="200" /></svg>

On the other hand if I try the same but this time using the notation described in svg web documentation http://svgweb.googlecode.com/svn/trunk/docs/UserManual.html (I'm importing the svg web file before any other javascript file), but nothing happens: 另一方面,如果我尝试相同的操作,但是这次使用的是svg网络文档http://svgweb.googlecode.com/svn/trunk/docs/UserManual.html中描述的符号(我先导入svg网络文件其他JavaScript文件),但没有任何反应:

   <script type="image/svg+xml"><svg><rect x="20" y="40" width="100" height="200" /></svg></script>

I made a script to execute all the javascript within the xmlhttprequest response text, by using eval. 我使用eval编写了一个脚本来执行xmlhttprequest响应文本中的所有JavaScript。 All the "normal" javascript works but this doesn't. 所有“正常”的javascript都可以,但是不能。 Does anyone have an idea of what I'm doing wrong? 有人知道我在做什么错吗?

Here is the XHR function: 这是XHR函数:

                            function loadXMLDoc(file, div){var xmlhttp;
                            if(window.XMLHttpRequest){
                                xmlhttp = new XMLHttpRequest();
                            }

                            else{
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                            }


                            xmlhttp.open("GET", file, true);

                            xmlhttp.onreadystatechange = function(){

                            if(xmlhttp.readyState==4 && xmlhttp.status==200){
                                var myresponse = xmlhttp.responseText;
                                var mydiv = document.getElementById(div);
                                mydiv.innerHTML=myresponse;

                            }

                            if(xmlhttp.status==404){
                                document.getElementById(div).innerHTML="<h1>File not found</h1>";
                            }
                            }
                            xmlhttp.send();

                                                                                            }

Here is an example of one of the php files loaded using xhr (I don't include the php classes that will in fact generate the svg): 这是一个使用xhr加载的php文件之一的示例(我不包括实际上会生成svg的php类):

   echo '<script type="image/svg+xml"><svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" version="1.1" baseProfile="full">
   <rect x="0" y="0" width="60" height="60" style="stroke: green;"/>
   <rect x="25" y="25" id="myRect" rx="0.6" ry="0.6" width="150" height="150" fill="green"   stroke="yellow" stroke-width="8"/>
   </svg></script>';

   ?>

In the case of the php file if remove the tag it will work by using the native support for svg but svg web won't work. 在php文件的情况下,如果删除标记,它将通过使用对svg的本机支持来工作,但svg网络将无法工作。

Thanks in advance. 提前致谢。

I haven't used SVGWeb but this answer is based solely on the link you provided for the documentation. 我没有使用SVGWeb,但是此答案仅基于您为文档提供的链接。

Firstly an explanation of why your approach doesn't work. 首先说明为什么您的方法不起作用。 Without reading the code, I'm guessing that how SVGWeb work is to parse the HTML page for the funky <script type="image/svg+xml"> tag and convert them to flash if necessay on page load. 在不阅读代码的情况下,我猜测SVGWeb的工作方式是解析HTML页面中的时髦<script type="image/svg+xml">标记,并在需要页面加载时将其转换为Flash。

Obviously, this means that any content added to the page won't get parsed since the page load event fires only once. 显然,这意味着添加到页面的任何内容都不会被解析,因为页面加载事件只会触发一次。 That's why your approach doesn't work. 这就是为什么您的方法行不通的原因。

Fortunately SVGWeb does provide an API to render SVG objects in IE at runtime. 幸运的是,SVGWeb确实提供了一个API,可在运行时在IE中呈现SVG对象。 It's a method called: svgweb.appendChild() (search the document). 这是一种称为svgweb.appendChild()的方法(搜索文档)。 Unfortunately the svgweb.appendChild method does not accept a string as an argument but needs a properly parsed svg object. 不幸的是, svgweb.appendChild方法不接受字符串作为参数,而是需要正确解析的svg对象。 Fortunately the documentation explains how to get that object at run time. 幸运的是,文档说明了如何在运行时获取该对象。 But you can't do it with XMLHttpRequest . 但是您不能使用XMLHttpRequest做到这一点。

Here's how the documentation says you can do it (read the Dynamically Creating an SVG OBJECT section): 文档说明您可以执行以下操作(请阅读“ Dynamically Creating an SVG OBJECT部分):

var obj = document.createElement('object', true);
obj.setAttribute('type', 'image/svg+xml');
obj.setAttribute('data', 'rectangles.svg'); // <-------- this is where you
obj.setAttribute('width', '500');           //           load the file from
obj.setAttribute('height', '500');          //           your server

// And this is how you add the SVG object to your document:
obj.addEventListener('load', function() {
    svgweb.appendChild(obj, document.body);
}, false);

Note that it doesn't have to be document.body . 请注意,它不必是document.body It could be a div or a table etc. 它可以是div或表等。

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

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