0

I want to use window.location in MarkupTemplateEngine, but am getting an error cannot get property location on null object.

My code is something like this:

...
div(){
   def link = "${window.location.href}"
   a(href:"localhost:8080?link=${link}", 'My link')
}
...

However link isn't rendered and i'm getting the error in console trying to access property of null.

With that said, I'm able to get full-fledged window object in browser console.

Clayn
  • 17
  • 1
  • 7
  • 2
    The [`window.location`](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) is a JavaScript object which is available inside the browser. So you have to invoke a JavaScript function to __bind it to your link-element at runtime__ (when the HTML document is loaded inside the browser). Only after the browser window has loaded your HTML, JavaScript can access this HTML as DOM inside `window.document` and return a URL inside `window.location`. – hc_dev Sep 09 '21 at 18:49
  • Your question is missing many details, please read [ask]. What was the error-message exactly? Did you do research? E.g. find a [similar question already answered](https://stackoverflow.com/questions/13131464/using-window-location-with-a-simple-html-link). – hc_dev Sep 09 '21 at 18:50

0 Answers0