Karrigell/Insert an image, a stylesheet, a Javascript in a document

From Wikibooks, open books for an open world
Jump to navigation Jump to search

If you have a document (image, JavaScript, CSS stylesheet) and you want to load them in a page, you pass an argument "src" or "href" to the tags <IMG>, <SCRIPT> or <LINK rel="stylesheet">

If this argument is a relative URL, remember that the script URL has the form http://host/myscript.py/func. So if the document to load is in the same folder as the script, the relative URL must begin with ../ : the relative URL ../foo.js will be resolved as the absolute URL http://host/foo.js

If you forget the leading ../, the relative URL foo.js would have been resolved as the absolute URL http://host/myscript.py/foo.js, resulting in a "File not found" error

The same goes for images and stylesheets :

def index():
    style = LINK(rel="stylesheet",href="../default.css")
    body = IMG(src="../images/books.png")
    return HTML(HEAD(style)+BODY(body))