Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Netbeans complain about this document write syntax in my Javascript?

In my Javascript code, I have the following line:

document.write('<style type="text/css">@import "style_mobile.css";</style>');

Netbeans seems to hate this, and gives me an error message that says:

XHTML element "style" not allowed as XHTML element "script" in this context.

Just to see if it would make a difference, I changed the line to use double quotes on the outside:

document.write("<style type=\"text/css\">@import \"style_mobile.css\";</style>");

Which only succeeded in changing the error to this warning:

Open quote is expected for attribute "{1}" associated with an element "type"

As far as I can tell, neither single nor double quotes impacts the fact that the code works, so I'm not sure why Netbeans is making an issue of it. I could ignore Netbeans' warnings, but I'm aspiring to have my code as clean and standards compliant as possible. There are lines similar to the above throughout my code, and Having all the yellow and red markers is an eyesore.

My current doc type is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

What would be the right format for the document.write function so that it acheives what I'm trying to do but does so without Netbeans throwing warnings and errors at me? Or is the code legitimate, in which case is there a way I can tell Netbeans to leave me alone?

like image 338
Questioner Avatar asked Dec 06 '25 05:12

Questioner


1 Answers

For some reason it's not allowing me to comment, so I'll put this comment here even though I don't believe it's necessarily an answer.

First, the document.write() shown isn't going to work with all double quotes. Quote the entire string with, say, double quotes, then use single quotes for the two sub quotes within the full string.

Insofar as the contents of the document.write(), I'm not familiar with the @include, but I'd make a stab at what I think you're trying to do with:

document.write ("<link rel='stylesheet' type='text/css' href='style_mobile.css'>");
like image 74
Terry Avatar answered Dec 07 '25 19:12

Terry



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!