1

I have to send a base_url of an flask function to js file since the url would be dynamic.

I was able to create the base_url for a particular flask function like

url_of_this_function = request.base_url

root_url = url_of_this_function[:url_of_this_function.rfind('/')]

pointing_url = str(url_for('filter_student'))

filter_pointing_url = ''.join([root_url,pointing_url])

I pass it to the html page through render_template

return render_template('mypage.html',\
        student_filter_link=filter_pointing_url,student_state_list=state_list)

mypage.html

<select id='student_state_selected' style='display:inline-block' onchange='get_student_filters({{ student_filter_link }})'>
     <option disabled selected>Select</option>
     {% for item in student_state_list %}
     <option value="{{item}}">{{item}}</option>
     {% endfor %}
</select>

when I try to read this in my js script it doesn't work.

function get_student_filters(filter_link) {
// Do my stuff
}

Is this the correct method to pass the url to the js? Is there a better way or am i doing something wrong here?

1
  • 1
    Hi, in your HTML file, add a script tag and in that, write the following js code const student_filter_link = {{student_filter_link}} Commented Nov 23, 2019 at 17:46

1 Answer 1

2
<select id='student_state_selected' style='display:inline-block' onchange='get_student_filters({{ student_filter_link }})'>
     <option disabled selected>Select</option>
     {% for item in student_state_list %}
     <option value="{{item}}">{{item}}</option>
     {% endfor %}
</select>

I just had to change onchange='get_student_filters({{ student_filter_link }})' to onchange='get_student_filters("{{ student_filter_link }}")'

It worked!

Hope it helps someone.

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

1 Comment

I think that is best to use a Html tag in the header, like the <base> tag. So you can get the url using document.getElementsByTagName("base")[0].href in Js. but don't take it as a recommendation, but as an alternative option :)

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.