2

I need to open a new tab using ClojureScript.

(js/window.open "http://localhost/go/somewhere")

I get the following error: Uncaught TypeError: window.open is not a function

It doesn't help setting it because nothing happens and I assume it is because it is a function and not a variable.

(set! js/window.open "http://localhost/go/somewhere")

I know it is possible because I got it right initially. I've since forgotten what I've done.

Edit: Also tried:

(set! js/window.location.open endpoint)

(set! (js/window.location.open -location) endpoint)

(set! (.. js/window.location.open -location) endpoint)
Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55
  • The syntax you are using in your very first attempt is correct. What version of CLJS are you using? – cfrick Nov 12 '19 at 08:33

2 Answers2

3

With your attempts with set! you have accidentally "overridden" window.open in your browser window; so window.open is actually no longer a function, that can be called.

Make sure you don't have a set! call before you call window.open and reload your page. The syntax you are using with (js/window.open ,,,) is correct.

(js/window.open "https://example.com") ; works
(set! js/window.open "https://example.com")
(js/window.open "https://example.com")
; ⇒ TypeError: window.open is not a function
cfrick
  • 35,203
  • 6
  • 56
  • 68
2

This works for me from a CLJS repl:

 (.open js/window "https://www.clojure.org")

but I had to make sure popups were allowed:

enter image description here

Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149