Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2013, 11:59 AM   PM User | #1
tumpelo
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tumpelo is an unknown quantity at this point
svg-elements in xml via XHR, putting a single element as an inline code to html5

Code below. 20 svg-elements within a XML-document got from a server via asynchrounous XMLHttpRequest, put into the table svgs (svgs.length == 20, OK). I'd like to put one randomly selected svg-element from the table to html5-page as a inline code. So I think I would need to have single svg -element as a string, which could be inserted to the html5-page. How to do this? And yes, I know that with innerHTML or with jQuery's .load()-method, but what I should the give as a parameter?

Code:
function showImage() {

if(xhr.readyState == 4){

if(xhr.status == 200){

if(xhr.responseXML && xhr.responseXML.childNodes.length > 0){
res = xhr.responseXML;
svgs = res.getElementsByTagName("svg");
alert(svgs.length);

}else{ 
document.getElementById('container').innerHTML = xhr.responseText; 
}
}else{
$('#container').load(toText(xhr.status));
} 
}
}
tumpelo is offline   Reply With Quote
Old 02-25-2013, 08:57 PM   PM User | #2
sbhmf
New Coder

 
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 40
Thanks: 3
Thanked 1 Time in 1 Post
sbhmf is an unknown quantity at this point
On a hunch, I wonder if a problem exists when your code attempts to access elements by tagname from a returned text-stream that has not yet been deserialized into nodes.
sbhmf is offline   Reply With Quote
Old 02-25-2013, 10:34 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
there is no command to get the source of XML nodes. (they are killing XMLSerializer soon)


you MIGHT be able to simply grab the node and append it to your HTML element.
you might need to use importNode depending on the strictness in use.

otherwise maybe a temp div will work?


Code:
var d=document.createElement("div")
d.appendChild(res.getElementsByTagName("svg")[0]);

alert(d.innerHTML);
or, the worst-case:
Code:
var d=document.createElement("div")
d.innerHTML=res.responseText;

var svg1=d.getElementsByTagName("svg")[0];

alert(svg1.outerHTML);
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, svg, xml, xmlhttprequest

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 PM.


Advertisement
Log in to turn off these ads.