3

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?

Questioner
  • 6,379
  • 15
  • 51
  • 87
  • What's your DOCTYPE? Also check here http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs Jul 27 '13 at 07:23
  • @elclanrs, thank you for responding. I've added my DOCTYPE to the question. I've also looked at the page you linked to. I'm not at all a pro at Javascript, so I'm finding the issues of modifying the "Dom" a little beyond me. All I really want to do is be able to, for example, include the CSS conditionally. If there's a better way to do that than document.write, then I would gladly just use that. – Questioner Jul 27 '13 at 07:27
  • I think that `type` attribute of style is getting messed with NetBeans. It is expecting `script` tag. – Heart Jul 27 '13 at 07:35
  • @Heart: Thank you for responding. If I use a ` – Questioner Jul 27 '13 at 07:56

2 Answers2

1

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'>");
Terry
  • 1,389
  • 2
  • 11
  • 25
  • Thank you for the suggestion. Unfortunately, Netbeans still hits me with largely the same errors whether I use ` – Questioner Jul 27 '13 at 07:48
1

I resolved this issue by moving away from the use of document.write entirely and appending nodes and elements to the document tree using Javascript code.

So, instead of:

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

... I use this instead:

var css = '@import "style_mobile.css";'
var header = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.appendChild(document.createTextNode(css));
header.appendChild(style);

Yes, it's more lines of code and work, but I think it makes for a more stable and reliable code.

Questioner
  • 6,379
  • 15
  • 51
  • 87