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?

4
  • What's your DOCTYPE? Also check here stackoverflow.com/questions/802854/… Commented Jul 27, 2013 at 7: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. Commented Jul 27, 2013 at 7:27
  • I think that type attribute of style is getting messed with NetBeans. It is expecting script tag. Commented Jul 27, 2013 at 7:35
  • @Heart: Thank you for responding. If I use a <script> tag, then the error gets more severe and it says "missing close quote", regardless of if I use single or double quotes (and after much checking and rechecking to make sure I am formatting the quotes correctly). Commented Jul 27, 2013 at 7:56

2 Answers 2

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'>");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the suggestion. Unfortunately, Netbeans still hits me with largely the same errors whether I use <style> or <link>.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.