1

I have Javascript that's run alongside URL rewriting, so it loads content from the wrong place if I use relative URL's. I need this to work on both a test server and a live server.

What's the correct solution here? Should I be using a Javascript function that forms the absolute URL's?

6
  • grab the current url, trim it, add the a/b/c Commented Oct 1, 2014 at 13:21
  • @starvator: Trim it to what exactly? If the website is in a virtual directory the root might be http://www.domain.com/approot/ :) Commented Oct 1, 2014 at 13:22
  • Maybe you need to use a variable $isLive = false; then check it with if($isLive) { define('base_url', 'url-live'); } else { define('base_url', 'url-test'); } Commented Oct 1, 2014 at 14:14
  • @TrueBlueAussie really? I have a site in a virtual directory and when I grabbed the url it included the aspx page.. Strange Commented Oct 1, 2014 at 14:34
  • @starvator: I meant what would you trim from a url where the website root is not at / e.g. http://www.domain.com/approot/somepage.aspx? Commented Oct 1, 2014 at 14:36

3 Answers 3

1

just define in your common file (header.php):-

<script> var baseUrl = '<?php echo BASE_URL;?>';</script>

and include your all js to below this code and access this var for use.

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

Comments

1

After including tag, use following. But remember you wont be able to use the variable in External

<script> 
    var siteBaseURL = '<?php echo BASE_URL;?>';
</script>

Because , PHP will ECHO any server side value and will send it as HTML , So PHP will send BASE_URL and such HTML With JavaSCRIPT will be included in browser.

But in any external JS Files like say

 <script src="example.js"/>  Here you wont be able to access the above variable.

3 Comments

Re your second comment: If the variable is declared in the global namespace, you will be able to access it from any script with window.siteBaseURL.
@TrueBlueAussie , yes correct . i had to say if he defined it as scope specific, then problems will occur
I think it was just the way it was worded that confused me :)
0

Whenever I develop on test vs live I only use "/css/location/cssfile.css" (this applies to any file) that way I don't have to modify anything.

Then when my HTML loads, it will call from the right location. The above translates into:

http://localhost/location/to/www/root/css/location/cssfile.css

or

http://test.com/css/location/cssfile.css

This will work for 50 sites:

/css/location/file.name

Would work for:

http://localhost/location/to/www/root
http://localhost/location/to/www/testsite1
http://localhost/location/to/www/testsite2
http://localhost/location/to/www/testsite3

etc

3 Comments

And you don't have to. Like I said, using the "/where/my/file/is" instead of the FULL ABSOLUTE PATH. You can develop for each site without actually hard coding it!
It doesn't matter. If you use /where/you/want/to/go/file.name then it doesn't matter where your root is at all.
A root relative path (such as your example) would always map back to the domain root. That is under browser control. I don't see how you would ever get the http://localhost/location/to/www/root/css/location/cssfile.css result you mentioned. You would more likely get http://localhost/css/location/cssfile.css

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.