Krishnan Shankar
Krishnan Shankar

Reputation: 942

Call a JavaScript Function from Flask

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.

Upvotes: 0

Views: 876

Answers (1)

MrPostduif
MrPostduif

Reputation: 61

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.

Upvotes: 1

Related Questions