henry4343
henry4343

Reputation: 3921

Cant include .js file in my php?

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

Upvotes: 0

Views: 573

Answers (4)

Pierre P
Pierre P

Reputation: 16

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.

Upvotes: 0

Yogesh Gupta
Yogesh Gupta

Reputation: 170

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>

Upvotes: 1

Akshat Singhal
Akshat Singhal

Reputation: 1801

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.

Upvotes: 0

zzlalani
zzlalani

Reputation: 24354

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 = '/';

Upvotes: 2

Related Questions