Http package question why form does not render without <br>

func main() {
	http.HandleFunc("/", foo)
	http.ListenAndServe(":8000", nil)
}

func foo(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	io.WriteString(w, `
		<form>
			<input type="text" name="q">
			<input type="submit">
		</form>
	 `)
}

codes above does not render form in chrome.

add <br> in the last line, it rendered form, why form does not render without <br> ?

func main() {
	http.HandleFunc("/", foo)
	http.ListenAndServe(":8000", nil)
}

func foo(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	io.WriteString(w, `
		<form>
			<input type="text" name="q">
			<input type="submit">
		</form>
		<br> 
	 `)
}

Just as assumption, but it may be related to the fact that both examples do not produce valid HTML and therefore the browser tries it’s best in compatibility modes.

i test this two version of code in index.html directly, but it rendered form both

Local files may be treated totally different than actual HTTP resources, as local files do not have any header information attached that may affect the initial rendering mode.

Unless you send valid HTML to the browser you may assume nothing. And even if, you may only assume the browser does it’s best. And this best may be different from what you expect.

Hello !

I tested your code in Chrome last version and the browser rendered the form

image

Maybe it’s a browser related issue. What version are you using ?

LE: I tested the first version.

did you test the codes without
?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.