0

I have a basic Flask app running, with the code in main.py hosting the flask app. I also have an index.html file that will get text input from the user. I can use request.form to access the input, and I can work with it successfully. However, I encountered a problem.

I need to call a JavaScript function that opens a new tab with a certain website. For example, if the user types in Wikipedia, I need to tell JavaScript to window.open("https://www.wikipedia.org"). The problem is, I can't find any working way to call a JavaScript function from a Flask application.

And before you mark this question as duplicate, the answer to the one Stack Overflow question on this I found did not even involve flask, and the other answer (using webbrowser.open_new_tab) opened the new tab on the server machine, not on the client machine (the one where the website is actually being accessed).

I know you can call a Python function from JavaScript, but is it possible to do it the other way?

Thank You in advance.

2
  • So you want to pass flask input variable inside that javascript function? Commented Jul 22, 2020 at 17:12
  • No, I want to call a JavaScript function (that opens a new tab) from a flask function. Commented Jul 22, 2020 at 17:18

1 Answer 1

1

One way to do this is by passing the url as a query string to the url containing the javascript. You can acces the query string in javascript and use it in the function that opens the window.

Sign up to request clarification or add additional context in comments.

2 Comments

I am a bit new to JavaScript... exactly how would you access the parameters of the current website?
let urlForNewWindow = new URLSearchParams(window.location.search).get('url') Lets say your url is {base_url}/index.html, you can add this to set the 'url' query parameter to wikipedia.com, ?url=http://wikipedia.com. So the final url will look something like this: {base_url}/index.html?url=http://wikipedia.com the urlForNewWindow variable will be equal to http://wikipedia.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.