Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use CSS '#' symbol in iframe

Tags:

css

iframe

Scenario

I have created a simple iframe which i am dynamically generating from user provided HTML and CSS.

JSfiddle

https://jsfiddle.net/jaup3769/1/

Problem

Whenever I use the '#' symbol, CSS parsing is stopped beyond that point.

p#para1{

}

won't parse beyond p#

Similarly,

p{
 color: green;
 background-color: #ff0000;
}

won't parse beyond background-color: #

What might be the issue?

like image 485
Abhi9 Avatar asked May 22 '26 08:05

Abhi9


1 Answers

Just use encodeURIComponent instead of encodeURI. From the MDN article:

encodeURIComponent differ from encodeURI in that it encode reserved characters and Number sign #

I think it even makes more sense to use it in the code you provided, since you are not encoding the whole URI, the data:whatever/charset:… part is already there…

  • MDN article
  • JSfiddle
  • related SO question
like image 96
helb Avatar answered May 24 '26 00:05

helb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!