2
votes

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)
2
The syntax you are using in your very first attempt is correct. What version of CLJS are you using? - cfrick

2 Answers

3
votes

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
2
votes

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