X
Tryk her for at gå til webstedets mobilversion.
Scheduled maintenance: Monday, March 30, between 3:30pm and 5:30pm UTC. This site will have limited functionality while we undergo maintenance to improve your experience. If an article doesn’t solve your issue and you want to ask a question, we have our support community waiting to help you at @firefox on Twitter

Supportforum

Getting the SVG document(contentDocument) using javascript inside the Object tag returns null

Skrevet

The following code work in Firefox v54, but stopped working on Firefox v68.4.1 (possibly earlier version as well).

index.html: <body>

<object type="image/svg+xml" data="test.svg", id="test_svg_id">

   Your browser doesn't support SVG
</object>

<script type="text/javascript" src="test.js"></script>

</body>

javascript: function init() {

 console.log("init()");

// Get the overall SVG defined in the Index HMTL page var svg_object = document.getElementById("test_svg_id");

// Get the SVG document inside the Object tag var svg_document = svg_object.contentDocument; }

Any idea why contentDocument is always null?

The following code work in Firefox v54, but stopped working on Firefox v68.4.1 (possibly earlier version as well). index.html: <body> <object type="image/svg+xml" data="test.svg", id="test_svg_id"> Your browser doesn't support SVG </object> <script type="text/javascript" src="test.js"></script> </body> javascript: function init() { console.log("init()"); // Get the overall SVG defined in the Index HMTL page var svg_object = document.getElementById("test_svg_id"); // Get the SVG document inside the Object tag var svg_document = svg_object.contentDocument; } Any idea why contentDocument is always null?

Valgt løsning

Zaak said

I am not using a server. I am directly opening the index.html. Is this a security issue that has been fixed in latest version of Firefox?

Oh, ok. That is the issue then. This is not supposed to work if you're just using file://. I guess it was probably added. It won't affect you if you actually publish the project though.

You can serve up the files locally with a variety of solutions. This one looks good if you are using node: https://github.com/http-party/http-server

I usually use python, so I run "python -m http.server" in the directory where all my files are. There are many solutions to serve up your files easily. I would highly recommend this as there are many more things that simply won't work when you open the html file directly.

Læs dette svar i sammenhæng 1
Citér

Yderligere systemdetaljer

Installerede plugins

  • Shockwave Flash 32.0 r0

Program

  • User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0

Yderligere information

Luk
Matthew Thomas
  • Top 25 Contributor
13 løsninger 90 svar

I believe either svg_object.innerHTML or svg_object.innerText should give you the output you are looking for.

I believe either svg_object.innerHTML or svg_object.innerText should give you the output you are looking for.

Ændret af Matthew Thomas den

Fandt du dette nyttigt?
Citér

Spørgsmålsstiller

I am trying to use interactive SVG and the contentDocument would give me access to all the object I can manipulate. I think something has been changed in later version of Firefox.

I am trying to use interactive SVG and the contentDocument would give me access to all the object I can manipulate. I think something has been changed in later version of Firefox.
Fandt du dette nyttigt?
Citér
TyDraniu
  • Top 25 Contributor
376 løsninger 2105 svar

Nyttigt svar

The contentDocument property on frames now returns null if the caller doesn’t subsume the document. This change affects the contentDocument property on the <frame>, <iframe> and <object> elements as well as the getSVGDocument method on the <embed>, <iframe> and <object> elements.

The contentDocument property on frames now returns null if the caller doesn’t subsume the document. This change affects the contentDocument property on the <frame>, <iframe> and <object> elements as well as the getSVGDocument method on the <embed>, <iframe> and <object> elements.
Fandt du dette nyttigt? 1
Citér

Spørgsmålsstiller

I am new to javascripts. Can you please give me an example?

I am new to javascripts. Can you please give me an example?
Fandt du dette nyttigt?
Citér
Matthew Thomas
  • Top 25 Contributor
13 løsninger 90 svar

Oh, I get it. Of course, you want to access the external file and not the "Your browser doesn't support SVG" text...

You are still on an old version of the browser.

Do you have this example hosted somewhere that we can take a look at? This object link is not going to work if you open the html file directly (i.e. with file://). Are you serving this up on a local server?

Oh, I get it. Of course, you want to access the external file and not the "Your browser doesn't support SVG" text... You are still on an old version of the browser. Do you have this example hosted somewhere that we can take a look at? This object link is not going to work if you open the html file directly (i.e. with file://). Are you serving this up on a local server?

Ændret af Matthew Thomas den

Fandt du dette nyttigt?
Citér

Spørgsmålsstiller

I am not using a server. I am directly opening the index.html. Is this a security issue that has been fixed in latest version of Firefox?

I am not using a server. I am directly opening the index.html. Is this a security issue that has been fixed in latest version of Firefox?
Fandt du dette nyttigt?
Citér
Matthew Thomas
  • Top 25 Contributor
13 løsninger 90 svar

Valgt løsning

Zaak said

I am not using a server. I am directly opening the index.html. Is this a security issue that has been fixed in latest version of Firefox?

Oh, ok. That is the issue then. This is not supposed to work if you're just using file://. I guess it was probably added. It won't affect you if you actually publish the project though.

You can serve up the files locally with a variety of solutions. This one looks good if you are using node: https://github.com/http-party/http-server

I usually use python, so I run "python -m http.server" in the directory where all my files are. There are many solutions to serve up your files easily. I would highly recommend this as there are many more things that simply won't work when you open the html file directly.

''Zaak [[#answer-1289168|said]]'' <blockquote> I am not using a server. I am directly opening the index.html. Is this a security issue that has been fixed in latest version of Firefox? </blockquote> Oh, ok. That is the issue then. This is not supposed to work if you're just using file://. I guess it was probably added. It won't affect you if you actually publish the project though. You can serve up the files locally with a variety of solutions. This one looks good if you are using node: https://github.com/http-party/http-server I usually use python, so I run "python -m http.server" in the directory where all my files are. There are many solutions to serve up your files easily. I would highly recommend this as there are many more things that simply won't work when you open the html file directly.
Fandt du dette nyttigt? 1
Citér

Spørgsmålsstiller

Thank you for your help.

I'll update you tomorrow if this solves my problems.

Thank you for your help. I'll update you tomorrow if this solves my problems.
Fandt du dette nyttigt?
Citér

Spørgsmålsstiller

I can confirm that directly opening the index.html in a web browser (using file://) no longer work.

As per Mathew's suggestion, I used local webserver by following method: 1. Ensure python is installed 2. Open Command prompt 3. Change directory to location of index.html in the Command prompt 4. Typing "python -m http.server" to start the server 5. Type "localhost:8000" in the Firefox web browser

I can confirm that directly opening the index.html in a web browser (using file://) no longer work. As per Mathew's suggestion, I used local webserver by following method: 1. Ensure python is installed 2. Open Command prompt 3. Change directory to location of index.html in the Command prompt 4. Typing "python -m http.server" to start the server 5. Type "localhost:8000" in the Firefox web browser
Fandt du dette nyttigt?
Citér
Stil et spørgsmål

Du skal logge ind på din konto for at svare på et indlæg. Start et nyt spørgsmål, hvis du ikke har en konto endnu.