Trouble including .js file into php - php

I am trying to include a .js file into a php file.
My folder structure looks like this:
root
---js (FOLDER)
------file.js
---blog (FOLDER)
------index.php
------js (FOLDER)
---------blog.js
If I am using this:
<script type="text/javascript" src="../js/blog.js"></script>
it works just fine.
What I can't seem to do is include file.js from under the root's "js" directory.
I've tried everything I can think of but I just can't seem to make it work.

<script type="text/javascript" src="/js/file.js"></script>
would reference the javascript file in the top level js directory

To access the blog, is a user going to www.domain.com or www.domain.com/blog?
If /root/blog is your DOCROOT in your web server, the /root/js directly likely isn't web-accessible. The location you're referencing MUST be accessible to the client's browser.
An easy way to test this is to enter the path to the JS file directly into your browser.

it looks like blog is in a subdirectory of the folder index.php is in. Discard the ../ bit.
<script type="text/javascript" src="js/blog.js"></script>
<script type="text/javascript" src="../js/file.js"></script>

Related

Root directory path problems

I am working on a website for a client and I am using xampp to serve and work on locally.
The site is loading fine at localhost/site but the resources are not being found. The previous developer used absolute paths in the index.php to define resources like so:
<script href="/includes/javascript.js">
The site fails to load javascript.js because it looks at:
localhost/includes/javascript.js
When it NEEDS to be:
localhost/site/includes/javascript.js
index.php is located here:
localhost/site/index.php
It works fine on the client server. What am I missing here?
A URL starting with / is always absolute, you need a relative path here. So use
<script href="includes/javascript.js">
in your index.php or all files located in localhost/site or use
<script href="/site/includes/javascript.js">
for use in all files.
You have a two mistake use src not href and remove / from start. try like that
<script type="text/javascript" src="includes/javascript.js"></script>

Path troubles when require or include file in PHP

I have directory structure like this:
- index.php
- templates(directory)
- login.php
- header.php
- js(directory)
- jquery.min.js
I include script jquery.min.js in header.php, then I include header.php in login.php, but the path to jquery.min.js brokes. How I try to solve it by replace this:
<script src="js/jquery.min.js">
to this
<script src="<?php echo dirname(__FILE__)?>js/jquery.min.js">
but then I get this path to resourse:
http://localhost/var/www/auth/js/jquery.min.js
And again 404 error. Can somebody explain to me how I can solve this issue.
js's script files work in context of web-root. as long as your web-root is where your index.php is then <script src="/js/jquery.min.js"> should work
You use <script src="js/jquery.min.js"> which is a relative path. so, if you call login.php browser requests js relatively to it: templates/js/jquery.min.js which doesn't exist, obviously
What you're trying to do is find the path of the javascript file relative to the root of your application, rather than any specific php file. So, if you change the script tag to begin with a /, it'll serve from the root of your application, regardless of where the php files are.
Changing it to <script src="/js/jquery.min.js"> will serve it from http://localhost/js/jquery.min.js regardless of where in your structure your php files are.

Relative paths and includes

So I have some php files that I'm trying to include with no luck.
I have the root directory, and off that I have a directory called aboutus. In the aboutus directory, I'm trying to include a css file from the root. So I use this code:
include $_SERVER['DOCUMENT_ROOT'] . "/meta-head.php";
The include works fine, but in the meta-head.php there are some calls to files, one of which is:
<script src="_assets/js/navbar.js"></script>
The problem is that it's trying to access this from root/aboutus/_assets/js/navbar.js (which doesn't exist), when I want it to acccess it at root/_assets/js/navbar.js.
What am I doing wrong so that it won't access the file relative to the root?
Thank you!
use absolute path or use this:
<script src="/_assets/js/navbar.js"></script>

JS Include inside included .php file

I have included a PHP file on my mainpage, inside the php file there's another include of a .js file, the included php file is in a different directory than the mainpage, so the included .js file can't be found.
To visualize:
Mainpage - Root
Included PHP file -File A located in Folder A
Included JS file in File A is located in Folder A
But it's looking for the .js file in Root instead of Folder A
How do I get the path right ?
Also it should be dynamic, I might not know the actual name of Folder A
Use the .. notation to traverse folders
the path would be '../script.js'
Site:
www.yoursite.com
Folders:
/index.php
/lib/header.php
/contacts/index.php
/js/main.js
Let's say you want to inclue your /js/main.js file inside /lib/header.php in order to work wherever you include /lib/header.php, for example in /index.php and /contacts/index.php.
You would have the following inside /lib/header.php :
<script src="http://www.yoursite.com/js/main.js" type="text/javascript"></script>
if include.php and xyz.js are in the same directory. than
include .js file in include.php like that--
<script type="text/javascript" language="javascript" src="xyz.js"></script>
if it is not working than u should use Relative Url/SITE URL like that--
<script type="text/javascript" language="javascript" src="http://localhost/folder/xyz.js"></script>

Problems with links not working on local server

I'm trying to set up my site in Dreamweaver CS5 to work with my local server, and I'm having issues with document relative links.
I've got a structure on my HD like this
website
_Common
header.php
_css
twoColFixRtHdr.css
index.php
and the same structure mirrored on my local WAMP server, except on the local server the site is in a subfolder, so it's something like www/website/
The problem is this line inside header.php
<link href="../_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
That looks correct to me pathwise, but on the local server it cannot find that css file from the header.php
If I change it to
<link href="/website/_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
or
<link href="_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
It works fine, but I want to use document relative links if I can, any ideas?
This sounds clear to me, since I'm sure that you include the header file in the index.php file. So the path for the css files is set relatively to the index.php.
you may achieve it by several ways.
one of them: you can setup a VirtualHost (in httpd.conf) and point your subfolder as root folder for host.
If header.php is being included in index.php, the relative link ../_css/ in header.php won't work because index.php is at the same level as _css.
For all intents and purposes, once it's been included in index.php, to the browser, the content in header.php is now simply part of index.php, so all paths need to be relative to index.php.
Ie:
index.php
/my_include_folder
- header.php
/_css
- style.css
Once I add <?php include('my_include_folder/header.php); ?> to index.php, the links to css files, js, and hyperlinks in header.php should be relative to index.php.
Hopefully that makes sense.

Categories