0

Trying to get this Script to open with window.open, any ideas?

<script type="text/javascript">
    function get_action(form) {
        form.action = 'http://'+document.getElementById("address").value;)
}
</script>

I need to be able to somehow put it after "form.action ="

Zeon Timar
  • 15
  • 3
  • 1
    Why is there a `)` at the end of that line? How is `window.open` related to it? What isn’t working? – Sebastian Simon Apr 02 '21 at 05:19
  • Must have been an accident, but I want the result of form.action to open in a window.open, how can I do that? Ex. form.action = window.open('http://'+document.getElementById("address").value;) – Zeon Timar Apr 02 '21 at 05:22
  • That semicolon cannot be inside function arguments. `window.open` returns a Window object, whereas `form.action` expects a string. It’s still unclear what `form.action` has to do with `window.open`. The `action` of a `
    ` is a URL (as a string) where form data gets sent. If you want a custom submit function, then use the [`submit` event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event).
    – Sebastian Simon Apr 02 '21 at 05:28
  • After it opens a window, do you need it to post any form data? Or do you want it to just open a window with a specified url? – Zak Apr 02 '21 at 05:29
  • Hey thanks heaps for the replies, @Zak I just need the window to open where the form is linking to, so if the form results in example.com/?test=1 I want that exact url to open in the new window. – Zeon Timar Apr 02 '21 at 05:38

1 Answers1

1

That function should be the correct way of changing it, however you should make the code run after the form is submitted

window.onload = function () {
    const form = document.querySelector("#myForm") // finds element with id of form
    form.addEventListener("submit", function (e) {
        e.preventDefault() // Stops the form from redirecting
        form.action = `http://${document.getElementById("address").value}`
        form.submit()
    })
}

If you want it to just open in a tab without submitting any form data you can use window.open()

window.onload = function () {
    const form = document.querySelector("#myForm") // finds element with id of form
    form.addEventListener("submit", function (e) {
        e.preventDefault() // Stops the form from redirecting
        let url = `http://${document.getElementById("address").value}`
        window.open(url)
    })
}

And if you want it to open in a new window change the window.open(url) towindow.open(url, "_blank", "location = yes")

Zak
  • 92
  • 6
  • @ZeonTimar Just read your comment about wanting it to display the ?= part to, I don't think that's possible with a form submission so you'd probably have to redirect them to that link, I'll add that method to the answer – Zak Apr 02 '21 at 05:40
  • Oh okay I get you, thanks again for all the help, as you said your code does work however I would like the end result to show the url in a new window rather than just opening it in a new tab. – Zeon Timar Apr 02 '21 at 05:43
  • Does this help you?https://stackoverflow.com/questions/14132122/open-url-in-new-window-with-javascript – Zak Apr 02 '21 at 05:45
  • Hey that worked for opening it in a separate tab which is great & it used the submitted URL, but could it work as a Mini Window that opens up while still on the original page it got submitted from. – Zeon Timar Apr 02 '21 at 05:47