0

Suppose that your domain is domain.com and you have the following directories and files

- index.php
<panel>
- panel.php
<css>
- style.css
<js>
- jQuery.js

In index.php you may call a css file like "css/style.css" or "domain.com/css/style.css"
In panel.php you may call a js file like "../js/jQuery.js" or "domain.com/js/jQuery.js"

What is the best method to use? css/style.css or domain.com/css/style.css ?
Does it make any difference? I am asking because in my case it will help me to use the domain.com/css/style.css but is it treated as an external file that may reduce perfomance?

6 Answers 6

1

you can even include all sources with //. means

<script src="//js/jquery.js"></script

the Browser takes http or https automaticly. So the Source can be included for both Ports without any Errors

Sign up to request clarification or add additional context in comments.

2 Comments

Whats the benefit of // instead of /?
// is an absolute Path like http:// or https:// but the Protocol is choosen by the Browser in coordination with the HTML Document
1

css/style.css will navigate to a folder relative to your current one. If you are in the original index.php, it will work. If you are in /subfolder/somepage.php, it will not work because it will try to get your css file from /subfolder/css/style.css.

domain.com/css/style.css will always work, provided you do not change domain name.

/css/style.css is the most generic: regardless of your domain, your page will always navigate from the website root to /css/style.css

1 Comment

None. There can be a speed difference factor if the css file is on a different physical server, but the way you refer to the file in your index.php will make no difference at all.
1

You can also use :

/css/style.css 

it is an absolute path, but in the same domain

1 Comment

css and javascripts are usually cached by browsers
0

As long as there is no http:// in front of it, it should be treated as a folder in every browser

3 Comments

I thought, domain.com/css/style.css is just a subdir.
No, domain.com... is the absolute link
Sorry for that. Then jerjer answer should be the right answer.
0

Does it make any difference?

Yes, primarily regarding maintainability. If a file i moved you'll need to recalculate all references for relative links (e.g., ../js/jQuery.js) while absolute links (e.g., http://domain.com/js/jQuery.js - or simply /js/jQuery.js) can be left unchanged.

I am asking because in my case it will help me to use the domain.com/css/style.css but is it treated as an external file that may reduce perfomance?

No, as long as the host is the same the client (browser) should treat it as a "local" link (none of which should directly affect performance, though).

1 Comment

@mtopia Yes. domain.com/css/style.css is a relative reference (pointing to ./domain.com/css/style.css where . is the current directory), while http://domain.com/css/style.css is an absolute reference. From the imaginary page located at http://domain.com/subdir/page.ext, domain.com/css/style.css will point to htto://domain.com/subdir/domain.com/css/style.css while http://domain.com/css/style.css will point to just that.
0

For usual I call it with an absolut path like /css/style.css.

Doing so you don't have to worry about changing the domain name in some time (like moving vom dev to production) or from which file (index.php, /panel/panel.php) your calling.

Also be sure to not include a protocol scheme (like http://), if you would switch to https:// the browser would claim some unsafe content, for example.

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.