1

I tried to insert the Google ReCaptcha script to the page, but I couldn't select the global grecaptcha.

val script = dom.document.createElement("script").asInstanceOf[HTMLScriptElement]
script.setAttribute("src", "https://www.google.com/recaptcha/api.js")
script.setAttribute("async", "true")

script.onload = { (_: Event) =>
  println(Dynamic.global.selectDynamic("grecaptcha")) // --> got undefined
}

dom.document.body.appendChild(script)

In the Console window, I can get grecaptcha:

grecaptcha

The question is how to select global grecaptcha object?

Phuoc Nguyen
  • 541
  • 4
  • 4
  • `window['grecaptacha']` or simply use it as if you defined a global variable, though be careful of the TIME when you use it – Dellirium Jul 19 '16 at 10:29
  • Thanks for the answer, but I couldn't access that (this is ScalaJS environment, not usual JS). – Phuoc Nguyen Jul 20 '16 at 10:54
  • If you cannot access it through `window['grecaptcha']` then it is not a global object. (I had a typo in my previous answer, if you didn't copy-paste you should be fine. All globals are accessible throguh the window object. – Dellirium Jul 20 '16 at 14:20

1 Answers1

0

According to this answer in a vanilla JavaScript environment, you simply have an ordering problem. The steps should be

  1. Append the script to the document
  2. Set the onload event
  3. Set the src attribute
val script = dom.document.createElement("script").asInstanceOf[HTMLScriptElement]
dom.document.body.appendChild(script)
script.onload = { (_: Event) =>
  println(Dynamic.global.selectDynamic("grecaptcha"))
}
script.setAttribute("src", "https://www.google.com/recaptcha/api.js")
script.setAttribute("async", "true")
Community
  • 1
  • 1
sjrd
  • 21,805
  • 2
  • 61
  • 91