javascript - Why is getElementById failing for an SVG? -


i'm trying debug why

var mapwin = svgmapdoc.getelementbyid('map'); 

leaves mapwin still null. have set breakpoint , watch , have following confusing situation in ide watch window:

name: svgmapdoc.getelementbyid('mainsvg') value: null neame: svgmapdoc.childnodes[2].attributes["id"].nodevalue value: "mainsvg" 

(n.b. 'mainsvg' parent of 'map'.)

so element id "mainsvg" there in 'svgmapdoc', third child node, getelementbyid fails return it. why? how can fix it?

=========== edit ===========

commenters asking html. tricky quite complex, here fragments.

it's not code comes uk office national statistics. provide access data uk 2011 national census. 1 access method provide through soap api , give examples of how consume api on page ness data exchange v2.0. having trouble getting 1 example working. it’s under nde v2.0 excel client in zip nde hub client rest v2.0 (zip) 37 mb, "hub demo excel-based application allows user build repository of saved data, , view via svg thematic map". note have made changes sample.

the map <div> svg data added page these lines:

document.writeln('<div id=mappanel style="position:absolute;left:26%;top:7%; height=63%; width=50%;">'); document.writeln('<embed height=100% width=100% name=svgmap onload="svgmaploaded()" src="../boundaries/' + svgfile + '" type=image/svg+xml>'); document.writeln('</div>'); 

the svgfile variable set "la_12ub.svg" , file present in 'boundaries' directory. here how 'boundaries/la_12ub.svg' starts.

<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/css" href="external.css" ?> <!doctype svg public "-//w3c//dtd svg 20000303 stylable//en" "http://www.w3.org/tr/2000/03/wd-svg-20000303/dtd/svg-20000303-stylable.dtd"> <svg xml:space="preserve"  preserveaspectratio="xminymin meet" id="mainsvg" width="395" height="420" viewbox="0 0 900 650">    <g class="mapedge" opacity="0.2">       <rect x="0" y="0" width="100%" height="100%"/>               </g>   <svg xml:space="preserve" preserveaspectratio="xminymin meet" id="map" onload="init(evt)" width="100%" height="100%" viewbox="541512 -262080 7829 10000">       <g class="mapbackground" transform="translate(1000,500) scale(1, -1)">           <path id="00ja" d="m 533246,308907 534698,301468 539690,302984 540167,303513 539729,302866 534716,301443 527492,299370 527194,299301 522976,298284 523340,298070 522788,297938 522896,297322 522287,296297 522752,296256 523134,295665 522992,295319 522142,295666 521979,295371 521209,295267 520981,294831 520598,295324 519801,295441 519692,294834 520037,294424 519608,293815 519307,293871 519182,293126 517207,292629 517375,292088 516523,291182 516301,291471 515933,291255 515710,292076 514043,294384 513155,295603 513611,295848 513756,296170 513511,296320 512826,296386 512500,296931 512035,297564 510765,297353 510349,297748 509529,297818 509347,298690 508718,299559 507903,299503 507472,299058 506810,299452 503855,298555 503186,298398 503176,298820 502294,299230 501879,299841 502341,300260 502671,301563 502968,301637 503035,302936 503435,302942 503614,303338 503418,304478 502550,305148 502370,304967 501950,305791 502644,306453 503260,306347 503212,306752 503764,306896 504753,306844 504914,307391 507122,306620 507966,306784 508919,307439 510829,308187 511058,308184 512364,308996 512669,309872 514367,309757 516476,308975 517551,307531 517895,307411 520168,309004 520976,309160 521409,309326 522343,307609 523190,308534 525850,307595 525946,307970 528419,309965 529405,309387 530284,310000 530904,310008 531006,310370 531399,310254 532300,309733 533178,309331 533246,308907 z"/> 

the javascript code puts svg dom 'mapwin'

function svgmaploaded() {     if (document.svgmap && document.svgmap.contentdocument) {         svgmapdoc = document.svgmap.contentdocument;     } else try {         svgmapdoc = document.svgmap.getsvgdocument();     }     catch (exception) {         alert('neither htmlobjectelement nor getsvgdocument interface implemented');     }     initialiseifready(); } 

and code i'm having trouble

function initialise() {     var mapwin = svgmapdoc.getelementbyid('map');     fullextent[0] = mapwin.getattribute('viewbox').split(" ")[0]; 

if don't namespace svg file correctly ids don't work expect.

the solution therefore add xmlns="http://www.w3.org/2000/svg" attribute root <svg> element whatever other namespace declarations such xlink required.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -