Question

I am using Groovy 1.8 XML MarkupBuilder to build an HTML page which includes a <script> tag.

When I load the page in Firefox 18 I see a blank page instead of the expected results.

This seems to be due to the generated <script> tag not having a complementary </script> tag, even though there is no content to warrant the </script> tag. (See: https://stackoverflow.com/questions/69913/why-dont-self-closing-script-tags-work )

Sample Groovy code:

def builder = new groovy.xml.MarkupBuilder( out )

builder.html {
  head {
    script( type:'text/javascript', src:'//example.com/example.js' )
  }

  body {
    p("Hello...Newman.")
  }
}

If I examine the (blank) rendered page's HTML using Firefox's "View Page Source" (Ctrl-U), I see:

<html>
  <head>
    <script type='text/javascript' src='//example.com/example.js />
  </head>
  <body>
    <p>Hello...Newman.</p>
  </body>
</html>

Looking closely, I see the '/' on the <script ... /> tag is rendered in red, and the hover-over text on the '/' says "Self-closing syntax ("/>") used on a non-void HTML element. Ignoring the slash and treating as a start tag."

So, how do I generate valid HTML <script> tags using Groovy XML MarkupBuilder ?

Was it helpful?

Solution

If you define some empty content, then the script tag will have a separate closing tag:

script( '', type:'text/javascript', src:'//example.com/example.js' )

OTHER TIPS

Alternatively you could use

script(type:'text/javascript', src:'//example.com/example.js){mkp.yield("")}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top