javascript - Src attribute in golang templates -
when executing template on golang server, got issue src attribute in html file searches importing javascript file not in root location (server folder), below handled url. so, requesting src='/dir/file.js' having current location http://localhost:8080/handled/ create request http://localhost:8080/handled/dir/file.js.
package main import ("net/http"; "html/template") var templates = template.must(template.parsefiles("././dir/file.html")) type page struct { title string body []byte } func testhandler(w http.responsewriter, r *http.request) { page := page{"handled", nil} err := templates.executetemplate(w, "file.html", p) if err != nil { http.error(w, err.error(), http.statusinternalservererror) } } func main() { http.handlefunc("/handled/", testhandler) http.listenandserve(":8080", nil) } so, template file.html contains next line:
<!-- file.html javascript placed right within template file working, didnt managed work src insertation --> <script src="file.js"></script> ...the file.js sharing same directory file.html. tried different file locations , various paths combinations. seems i'm doing wrong way.
if browser requesting /handled/dir/file.js src must missing initial forwards slash. not go specific problem.
i see file beingness requested has src="/dir/file.js", in illustration show src="file.js", not sure have. however, problem still indicative of forgetting first slash, src="dir/file.js"
javascript html web go
No comments:
Post a Comment