Linux Server - Paths - javascript

On a Linux server, I am trying to access files in a separate folder on the same server, but can't quite seem to reach them. The PHP script is located in a subdomain folder:
/home/sofia/public_html/mail.domain1.com/index.php
and I am trying to load an image resource from a different folder, with absolute paths like these (JavaScript DOM):
document.body.style.backgroundImage = "url('/home/sofia/public_html/common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('/home/sofia/common-res/bgImage.jpg')";
or even with relative paths:
document.body.style.backgroundImage = "url('../common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('../../common-res/bgImage.jpg')";
Is there something wrong with the syntax, or are such references simply not allowed?
Thank you for your kind attention.

Local paths such as /home/sofia/common-res/bgImage.jpg will not work. You must either use full or relative URLs:
document.body.style.backgroundImage = "url('../../common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('http://example.com/common-res/bgImage.jpg')";
Both are acceptable, just make sure they resolve to a directory that is properly being served.

Just an update for the benefit of others. Support from my hosting company has informed me that for such servers where multiple domains reside on a single account, each domain or sub-domain in the public_html folder is treated as a separate domain, and thus cannot breach the cross-domain protocol.
However, since they are on the same server, a simple solution would be to create symlinks within each domain or sub-domain, to enable them to access local resources beyond their scope.
In any case, the solution offered by Yuval Adam and Prava works just as well. Thank you both.
It should also be noted, that depending on the host server configuration, referencing a http path would incur connection overheads, while relative references do not.

Related

Get URL and redirect before page load

So, i have an interesting situation. I've been working on re-organizing a directory on a website. I updated old files there's about 100 of them, they are in a new location. The old files have been taken down.
The problem I have is there are probably hundreds of people that have bookmarks directly to the URL of the old files. (e.i. "wahwah.com/subSite/pdfs/something.pdf") these files are 5 years old so they need to find the new ones anyways.
So instead of having a page for each individual file, Can I have something in the directory that used to house the files to watch for that URL and redirect to the new page?
It would watch for "wahwah.com/subSite/pdfs.." and redirect. Or maybe something in the main directory of this subSite to watch for the URL to have the /pdf path in it.
I know I can grab URLs in java script but that doesn't help me unless I can do what I stated above. I'm not sure how if at all I could do it in .NET. our servers support .NET because most of our site apps were made with it but I don't deal with those. I cannot use PHP, the servers don't use it.
I'm hoping JavaScript will be able to do it somehow, but it's something i've never tried before so just thinking about it i'm not sure I can. I'm not much for using JS libraries so Im not sure what is out there i've been searching a bit though.
I found Grunt but i'm not entirely sure how it works just yet. Just looking around maybe the file filter or matchBase. or some of the Global patterns.
If you have access to server, your best option is to set up redirect in there on wahwah.com/subSite/pdfs/ directory.
How to do this depends on if you're on IIS or unix.
In asp.net, 301 redirect is fairly efficient.
if (HttpContext.Contains("http://old.aspx"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("http://www.new.aspx");
}
Or in page load you can write:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://new.aspx");

How to get the current file path in javascript

can i get the full path of my html page through javascript eg My Index page is at
d:/somefolder/anotherfolder/index.html
so can i get this path
Thanks
You can only access your current file path relative to your host URL, you cannot access the folder path of that file.
This is restricted in JavaScript due to security reasons. If that was allowed, malicious scripts could easily read your server's internal folder structures, which is bad.
On a Local Machine it is not possible, however you could use:
document.location.href;
to get the URL, where your file is hosted
no you can't browser don't allow javascript to access the file paths due to security reasons

javascript window location get path of website to redirect to between localhost and actual domain

I want to be able to redirect my user on logout of my website back to the homepage/login, this would be easy to do if I just redirected to the website itself, IE http://www.example.com/ however, how could I determine the root folder path for my site when it's on localhost?
for example, some of my websites are in: localhost/folder1/websiteBackup/ and some are in: localhost/
what would the best way to combine it so one line of code will work for both the localhost and the domain name?
Use a relative path instead of an absolute one. For example, instead of
/somePage.html
you might write
../../somePage.html
If the script is shared and you don't know where it's going to be relative to the page, consider developing on separate <VirtualHost>s so that your environments match.
this is what works for me..try your luck
url:'<%=VirtualPathUtility.ToAbsolute("~/Include/ctnrls/Data.aspx") %>',
but I am mapping my virtual path in IIS
document.location =
document.location.href.indexOf('localhost') >=0 ?
document.location.href.split('localhost')[1] + 'index.htm' :
'index.htm';
I would suggest use some server side session variables (PHP/ASP) and assign root path to them. then you can use them any time you want.
session("rootPath") = "/RootFolder/your-filename" (for ASP/VBScript)
Later use them in you JS code to redirect.
var path = '<%= session("rootPath")%> (JS code)';
Check similar question I asked in here for more info on Relative path, Relative Root path and Absolute root path.

JQuery ajax .load() to reference to the correct url

I have a localhost with a few projects, so this project in VS2010 properties->web->use local IIS web server = 'localhost/project4/'. (I can't use the normal debug VS Development Server as some components won't work in my project) Anyhow, localhost is fine no big deal, so I go on coding.
Now, in ajax when I call .load('/Account/LogOn'); it gives me 'localhost/Account/LogOn' but what I really want is 'localhost/project4/Account/LogOn' because it is sitting in the project4 directory, not the root.
Any idea how to tell ajax I need that virtual directory prefix between domain name and the rest of the url?
EDIT --------------------------------------------
Thanks guys, combined with all your knowledge, I guess the best ways to do it are:
Include the js script into .cshtml server side and use "~/Account/LogOn/" let .net figure out the path.
Make a global var remove_me_debug_only_virtual_directory = "/project4/"; in js code. append it to the domain name. this way we don't have to pull .net into the water and write js code in .cshtml.
Move the project to localhost/ root if you can, in this case I can't, because other people at work wants to access this networked server and do demo.
If your JS code is in an MVC cshtml file, use it like this:
.load('#Url.Action("LogOn", "Account")');
The proper URL will be placed in the concrete JS code.
According to .net documents,
Absolute and relative path references in a server control have the following disadvantages:
Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.
Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.
To overcome these disadvantages, ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
So as described above, using ~ will lead you to the root of your web project using asp:
<asp:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg" />
For more information go to: Web Project Paths
Based on your update: Possible duplicate of: base url using jQuery
Where stated by #gregguida:
var l = window.location;
var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];
//all you need to do is work with l.pathname.split('/') to find what you need.
I am a java developer but the context is same in both case,
In your can follow the steps
Check the context of your server if root is "/" then you have to call something like,
.load('/project4/Account/LogOn');
If your application is root and you can calling this request from itself then
.load('Account/LogOn');

JS Ajax: local vs absolute path issue when using SEO friendly urls

here's the thing:
i built my site with SEO friendly urls...but i have problem now calling ajax files becaus eth epath is wrong and I cant set an absolute url when i call my files in background..
for instance my page is here (similar to StackOverflow..)
www.domain.com/product/123/this-is-a-product
but my javascripts functions, in folder /js,now they try to reach the files but they cant obvisouly because are set to relative path...
how can i solve this issue??
EDIT: Found this How to get the root path in JavaScript?
When you are using freindly urls, then you have to use path started with /. But then you are starting path from main path.

Categories

Resources