[removed] javascript function is not working

2019-09-07 01:50发布

问题:

I'm trying to use the document.write javascript function to set an external css file in my template, but, I want to do this via Twig just like this:

document.write('<link href="{{ asset('bundles/activos/css/app-orange.css') }}" rel="stylesheet" >');

But it does not work. Why this document.write function is not working? Can I put that Twig code in there?

Or what is the best way to pass a css route with Twig to a document.write javascript function?

Sorry if my english is not good. I hope you understand. Thanks.

回答1:

Try this

{% stylesheets
    "@ActivosBundle/Resources/public/css/app-orange.css"
    "%kerner.root_dir%/Resources/css/main.css"
%}
<link type="text/css" rel="stylesheet" media="all" href="{{ asset_url }}" />
{% endstylesheets %}


回答2:

Finally I realize the answer. The problem is that I was using the document.write() function inside of $(document).ready() function. My mistake was not to post the entire code here so you could see. I didn't know until now that I could not use document.write() function inside of the $(document).ready() function. Anyway, I want to thank you all for the comments and answers!