clojurescriptclojurescript-javascript-interop

How do I open a new window using ClojureScript?


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)

Solution

  • 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