tags:

views:

60

answers:

4

Hello. Alright so im done doing pages with frames, and know i splitted up my design to top.php and bottom.php. In top.php it's linking to style.css and ajax_framework.js and such.. Now this works great until i tried to include top.php & bottom.php in videos/index.php (another dirr), i did like this:

include "../top.php";
<-- and the originally index.php -->
include "../bottom.php";

Now when i enter the site, it wont load style.css, probably because it thinks that style.css needs to be in videos/ directory, same goes with ajax_framework.js that are both files are on the parent ../ directory.

Now what do i do? Some method or technique to solve this in a good way? So i dont have to copy style.css and ajax_framework.js into every directory i have, because then later if i want to change something i would need to change in all directories, which would be my last thing i want to do.. hope you got some nice techniques thank you

+2  A: 

You have at least three options:

  1. Make all your resources URL absolute URL. For instance, change <script type="test/javascript" src="scripts/foo.js"> to <script type="text/javascript" src="/scripts/foo.js">. If you can do it, it's by far the less intrusive solution.

  2. Set a BASEDIR constant before you include top.php and make every resource path relative to it:

    // in your file that includes top.php
    <?php define('BASEDIR', '..'); ?>
    // in top.php
    <?php if(!defined('BASEDIR')) define('BASEDIR', '.'); ?>
    <link rel="stylesheet" type="text/css" href="<?php echo BASEDIR; ?>/style.css"/>
    
  3. Add a <base> HTML tag in your <head> (this one sucks, don't do it):

    <base href="/foo"></base>
    <!-- now all srcs, hrefs and whatnot are relative to /foo -->
    

    Its sucks because even anchors will be relative to it. So if you have a link like <a href="#foo"> on the page qaz.php, with the <base href="/bar">, the link will point to /bar#foo instead of qaz.php#foo.

zneak
LOL. I didn't see #1 there before until after I added my answer! Very zneaky.
webbiedave
thank you zneak!
Karem
the BASEDIR worked well!
Karem
@webbiedave: I promise it's only a coincidence.
zneak
A: 

Add a <base href="..." /> tag to top.php. The base tag defines the URI from which all relative URIs are resolved.

Details and example here: http://www.w3.org/TR/html401/struct/links.html#h-12.4

Jakob Kruse
+1  A: 

You didn't post your html but I'll assume that you aren't using absolute uri's. You're probably doing something like:

<link rel="stylesheet" type="text/css" href="css/style.css" />

When you should be doing something like:

<link rel="stylesheet" type="text/css" href="/css/style.css" />

webbiedave
A: 

The answer is easy:

In top.php i guess you have something like this:

<link type="text/css" href="style.css" rel="stylesheet" />

Just change it to be an absolute URL by adding a slash to the beginning, like this:

<link type="text/css" href="/style.css" rel="stylesheet" />

Now the browser will look for your stylesheet from the document root directory in an absolute path.

BTW, people reading your question cannot easily grasp the situation because you seem to refer to different posts. I believe it is better to describe the whole problem in one post because i did not find your previous questions.

David