简体   繁体   中英

Dynamic page embedding in HTML/JavaScript

I have a tag like this in my HTML page:

<embed src="http://..." style='...'>

Using Python-Flask I hand over a varying source address and store it in window.dynamicEmbedding . Now I want to change the src value dynamically on page load in a compact way. I imagine something this

<embed src=<script>document.write(window.dynamicEmbedding)</script> style='...'>

which is surely not possible. Is there anyway to realize this without a lot of coding?

It would depend on the logic you want for deciding which urls to serve. If you simply want a random url, that would be fairly easy in javascript. Simply place the urls in an array and use math.Random to select an index. Each time the page loads, a new random index can be generated, and a new url is selected to be passed to your embed.

Since you're using flask though, the easier way - particularly if you need more complex selection criteria than random - would be to write the selection logic into the python, and pass only the selected url to the template. You have a flask view like this:

import random

@app.route('/')
def video_page():
    urls = ['1','2','3','4','5','6','etc']
    url_selection = random.choice(urls)
    return render_template('video_page.html', url=url_selection)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM