0

I use <a href=Common/php/VideoPlayer.php> to jump to the other php file and in that php I want to include a .js file if I use this

<script src="Common/scripts/video.js"></script>

it will fail to load But if I use this, it will load

<script src="http://localhost/website/Common/scripts/jquery.js"></script>

I dont know what's the difference between those ? But I use <script src="Common/scripts/video.js"></script> in my first html, and it works, but if I click the hyperlink to jump to the other php file, it fails. Please help me.. Thanks a lot

1
  • In the first line u are using a relative path. This means your browser wil start looking for the file in the map where the file is located. In other words when u click the url u change the map from Common to Common/php and u'r browser will look for the script at Common/php/scripts Commented Dec 26, 2013 at 8:07

4 Answers 4

2

Try

<script src="../scripts/video.js"></script>

or

<script src="/website/Common/scripts/video.js"></script>

For most of my projects I use to keep a baseUrl variable in the config for non MVC projects

$baseUrl = '/website/';

and use them for calling local files like

<script src="<?php echo $baseUrl; ?>Common/scripts/video.js"></script>

and when I transfer the site to the server I change my $baseUrl accordingly for example

$baseUrl = '/';
Sign up to request clarification or add additional context in comments.

Comments

1

in your html use <base href.../> for writing web root path and then give relative path to scripts

Eg

<base href="http://localhost/website/"/>

<script src="./Common/scripts/video.js"></script>

Comments

0

You need to first understand the concept of PHP, HTML and JavaScript and how they are related. Javascript and HTML are client side code and PHP is server side.

When you are including the .js file in your php code, you are actually including the .js file in the HTML page generated from that PHP code.

Comments

0

The first one is a relative path, relative to your PHP file, while the second one is a fixed path relative.

I imagine that you have a foldered structure, like that :

  • firstfile.php
  • subfolder
    • secondfile.php
  • Common

With the first method, when opening firstfile.php, your browser will find the Common folder and its content. If you open subfolder/secondfile.php in your browser, it will look for the Common folder in the subfolder folder (because the path you gave is relative to the PHP file), and will not find it.

With the second method, the path to the Common folder (and its content) is fied for every file, so no issue.

Comments

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.