Jump to content
Search Community

Embed SVG using object tag

asoelist test
Moderator Tag

Go to solution Solved by Cassie,

Recommended Posts

Then on line 54 on my chrome browser lies the error. If you comment this line out, you get errors. Please refer to console on chrome browser.

 

Is it possible to embed SVG in object tag and use Javascript's contentDocument to manipulate the SVG image?

 

Thanks!

Screen Shot 2022-12-26 at 22.17.53 (2).png

Link to comment
Share on other sites

  • Solution

Hi there! What problem are you trying to solve by using an object? 

It's not a very common practise. It's best to just add your SVG code inline.

 

 

If you want to go ahead with this object approach, If you log out 'line1' in your demo, what do you get in return?
 

  • Like 1
Link to comment
Share on other sites

10 minutes ago, Cassie said:

Hi there! What problem are you trying to solve by using an object? 

It's not a very common practise. It's best to just add your SVG code inline.

 

 

If you want to go ahead with this object approach, If you log out 'line1' in your demo, what do you get in return?
 

 

11 minutes ago, Cassie said:

 

If you want to go ahead with this object approach, If you log out 'line1' in your demo, what do you get in return?
 

Thanks Cassie for your prompt reply.

 

I change the repeat from infinity (-1) to 0  and comment out line 54 on my vscode, I will get 150 lines of error. If the repeat is -1 (infinity), the errors will go on and on.

 

Somehow the chrome browser cannot read the SVG file because it is not inline. (it is on separate file). 

Screen Shot 2022-12-26 at 23.14.26 (2).png

Link to comment
Share on other sites

BTW Cassie, I have read briefly the link you posted. 

 

Basically, the solution is to asynchronously fetch the image and insert the element into html page using js script. This has the effect of injecting/inlining the SVG object into DOM html page. So you don't have to use the JS's contentDocument to manipulate SVG Object. 

 

I will try it tomorrow. Thanks for the links. 

  • Like 1
Link to comment
Share on other sites

I tried the fetch and insertAdjacentHTML approach and it worked! 

 

Saves me time on StackOverflow on finding CORS issues on my web backend. (need tweaking in my settings.py)

 

and most importantly the ability to dynamically inserting and removing objects on the frontend which is what I want.

 

Thanks again Cassie. 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...