[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get site domain within javascript

Posted on 2007-07-26
11
Medium Priority
?
14,595 Views
Last Modified: 2008-01-09
The website I work with uses mod_rewrite to create nicer urls, but a result of this is that we have to put the full path to images etc within javascript include files.

I would like to avoid doing this, so that the include files can be easily transfered to different websites without the need for modifying them.

Eg
If scripts.js is included from the following URL:

http://www.domain.com/prodname/prodsub/name.php

The following code would not work:

var arrowheadimg=["./ukimgs/arrowdown.gif"] //path to down and up arrow images

as it looks for the image at http://www.domain.com/prodname/prodsub/ukimgs/arrowdown.gif

where as the image is actually in:

http://www.domain.com/ukimgs/arrowdown.gif

NB:  I can't just put  few extra ./../../ on the image reference as the script will be used by many pages that have different number of params from the mod_rewrite and so varying length.

Thanks

0
Comment
Question by:dealclickcouk
  • 5
  • 5
11 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1200 total points
ID: 19572514
remove the dot
/ukimgs/arrowdown.gif
will be found as long as ukimgs are in the root of the web server
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19572522
If you REALLY need to calculate it:

var arrowheadimg=["http://"+location.host+"/ukimgs/arrowdown.gif"] //path to down and up arrow images

but that is plain silly
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 800 total points
ID: 19572578
remove the leading ./ in your paths, this making the path relative which is safe for any FQDN and script-path rewriting
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19572586
./ukimgs/ is the same as ukimgs/ and should look in
http://www.domain.com/prodname/prodsub/ukimgs/ too
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19572756
> ./ukimgs/ is the same as ukimgs/ and should look in
mplungjan, are you sure?
if used in a href= or src= attribute, ./whatever is a relative path, while /whatever is an absolute one
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19572788
Yes
/ukimgs is absolute and that is what he needs here
./ukimgs and ukimgs would both point at http://www.domain.com/prodname/prodsub/ukimgs/ and that is the problem in this case
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19572844
oops, read your comment to fast, hence my silly question, sorry
anyway, that relative thing has already be mentioned :)
0
 

Author Comment

by:dealclickcouk
ID: 19572918

Thx for the contributions, but now I'm confused, as you all seem to contridict each other.

it seems you're saying that removing the ./ will work, but then also that it will resolve to the full rwrite path, whereas I need the root path.

Just for clarification which solution is the correct one?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19572980
> .. but then also that it will resolve to the full rwrite path,
no, ./ is a relative path

> .. whereas I need the root path.
then you need to use /

Keep in mind that you need to know what the rewriting (mod_rewrite) reall does.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19573068
Not if all images are in http://www.domain.com/ukimgs
which is /ukimgs , e.g.

var arrowheadimg=["/ukimgs/arrowdown.gif"] //absolute path to down and up arrow images
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19574119
the relative path must match some physical one on the server, obviously :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question