Java Script to get localhost+htdocs folder - javascript

I have following url to be build,
http://localhost/myweb/cart/index.php
I want to get the http://localhost/myweb/ bit build dynamically.
To do that on my live web site which is http://www.myweb.com/cart/index.php I can use the following JavaScript code,
var http = location.protocol;
var slashes = http.concat("//");
var host = slashes.concat(window.location.hostname);
But how do I get my development environment to work since it has http://localhost/myweb/? If I run the above code it will give me http://localhost/ only.
Any suggestions?

window.location.pathname is the thing you search for.
I would suggest you to read the MDN description of window.location. Like everything else in MDN, this is also really straightforward and informative.
If you know that the URL has an unnecessary index.html part at the end you can:
var path = window.location.pathname.split('/');
path.pop();
path.join('/');
or you can slice it (since it is generally faster):
path.slice(0,path.lastIndexOf('/')+1)
EDIT:
Seeing your new question I can say that what you want can't be done consistently and safely by only the current URL.
You need the http://localhost/myweb/ part, which is the URL root of your application. In javascript you are able to get the protocol and domain of the url. On your live site these 2 match, but if your application resides in a subfolder (like the myweb folder at your localhost), this will fail.
What you need is to somehow identify the application URL (the URL root of your application).
The problem is that by only examining the URL, javascript cannot tell where your application resides.
Let's say you deploy your site to: http://localhost/myweb/site1/
You will have the following URL: http://localhost/myweb/site1/cart/index.php
Javascript can split your URL by the slashes (/) but it has no way of nowing how many subfolders it should select. For example from the URL above your application root can be any of the following: http://localhost/, http://localhost/myweb/, http://localhost/myweb/site1/, http://localhost/myweb/site1/cart/.
By an other approach (which I suggested first) you can drop the end of the URL (in your case the cart/index.php part). This will only work if your URL structure IS very rigid, so all the pages this script is executed on reside in one subfolder.
So it will break on the following URL: http://localhost/myweb/site1/gallery/old/index.php or similar.
Your best bet would be to make this a "config variable" in a separate file which you edit on every location it is deployed to.
Either as a PHP variable ($appRoot = "http://localhost/myweb/") which you generate the javascript with.
Or more simply a javascript variable (var appRoot = 'http://localhost/myweb/'). Make a separate js file, call it something like config.js, add the above line to it and reference it before your other javascripts.

Related

Confusion regarding Apache document root

In one of the applications that I am working on for my company, I came across a weird behaviour or maybe it's just my misunderstanding and I hope I can get some clarification.
The application is served by Apache and the root is : /company/client. For every page that I visit, for example https://11.11.11.11/index.phtml, it will actually point to the file in the server /company/client/index.phtml and so on. In one of the modules of the application, it contains a move_uploaded_file php function, and the target directory is /images/example/, when the page is run, the app is trying to go to the absolute server root /images/example/ instead of /company/client/images/example/.
Also the a new windowed opened up by window.open has an img tag having src='/images/exmaple/', this points to the server root instead of /company/client/images/example/, is this expected?
Am I missing anything, or is it something to do with Apache configuration?
Additional info:
The application is served as a virtual host in conf file, with DocumentRoot "/company/client/".
The page that is executing window.open and php function is used as an Iframe inside /company/client/index.phtml
sorry for my mistake.
Thanks for the help from the everyone especially Chris G, the problem was that the code is using a GET variable incorrectly, as a result the image name isn't passed. And I got confused because someone made a mistake in the code by moving image relative to the root folder which is incorrect. That made me think that PHP is also treating path like the client side which is a mistake. I'm guessing I can conclude that the web server document root only applies to everything client side, like the url, JS, HTML?

How can I find the current html filepage name using Javascript, on a LIVE-SERVER

I am going to deploy this page on an FTP
And I need to find out how I can detect the html file currently being viewed using JavaScript.
If I open the html file, it works just fine with this:
var fileName = location.href.substring(location.href.lastIndexOf("/") +1);
But, if I open it via my localhost adress, it has a null value. So I'm guessing I have to use some other method to extract the current html file name. Or is there a better approach to this?
Note: I am not going to use JQuery or anything like that.
EDIT:
I can get the filename if it isn't my index file.. If it's the index file I get nothing using the above code. Most likely since all I have in my adress bar is the localhost adress of the live-server?
The web deals in URLs, not file names.
Sometimes a URL will include something that looks like a file name, and sometimes that even maps on to a real file name on the server's hard disk.
When you type http://example.com/ then it might map that onto a file called index.html. Or maybe on to index.php. Or maybe it won't touch any file but will just use logic built into the web server application to determine what to respond with.
There's no way to know in the general case.
If your specific case, you know that the path / maps onto index.html, so you can write an explicit mapping in your JavaScript code.

open a pdf document from an url with only partial filename

I'm trying to use javascript to open a .pdf link on chrome for which I know the base URL and the beginning of the .pdf, however I'm missing the last caracters of the file.
I was thinking I could maybe loop through all the files located in the specific folder of the URL and check if one of them was starting like the file I wanted but couldn't get very far as I didn't succeed in getting the list of all filenames from that URL as I only found window.location.href but that doesn't refer to my URL but to where I'm at currently.
Even better I figured I could provide the start of an url with a wildcard and that it'd open the first document matching this, kind of the "like" in sql but it didn't work either.
StringToFind = "recordnumber123";
if(window.location.href.indexOf(stringToFind) > -1) {
filename = name_of_the_file_that_matched;
}
My base URL is something like:
"https://www.documents.com/records/"
my filenames are like
"recordnumber123_201810091102453651.pdf"
and I only know the part:
"recordnumber123" and that it is a .pdf.
The numbers after the underscore are added by the system as some sort of timestamp but I don't have a way to get them.
If this isn't possible, I also know the server on which these files are stored (for which I have the exact name) but I couldn't manage to open the file from chrome.
I tried to open it using
href="http://file://///https://www.documents.com/records/recordnumber123_201819124564.pdf"
but it redirects me to http://ww8.file.com/
and href="file://///https://www.documents.com/records/recordnumber123_201819124564.pdf"
but it doesn't launch anything. From what I read it seems that it's not allowed anymore to access files direclty like this from Chrome?
I can only use chrome and I cannot install specific plugins...

jQuery AJAX Cannot Find Local File in WordPress Installation

I'm having trouble with a WordPress plugin I've been working on. A JS file is loaded as a resource with each page/post opened, which in turn has a request to load the contents of an HTML file.
Being that the page/post directories change frequently, I'm having a difficult time making the jQuery dynamically pin down the location of the file (even though it's in the same location as the rest of the plugin resources).
An example:
jQuery('body').append('<section id="asub00LOAD"></section>');
var url = jQuery(location).attr('hostname');
var dir = url + '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';
jQuery('#asub00LOAD').load(dir);
That was placing the whole URL path to the file after the local install ("root.com/CHEETOS/" in this case):
After which, I did this, which works fine for the root directory only:
jQuery('body').append('<section id="asub00LOAD"></section>');
var dir = 'wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';
jQuery('#asub00LOAD').load(dir);
After you venture to another page, obviously the directory location is wrong again.
I tried to place some PHP into my JS file before so I could take advantage of the $plugins_url feature, but that became very convoluted and it's hard to track any errors without a PHP console to work from...
I hope someone here will have a solution for me!
The first example probably fails because there's no http:// (or //)
Use var url = "//" + location.hostname;
The second example should work everywhere if you use a root-relative path:
var dir = '/wp-content/plugins/adsenseunblock/html/adunblock.html #asub00AJAX';

Is it possible to retrieve text files from HTML app directory without HTTP request or <input>?

I'm working on an HTML/javascript app intended to be run locally.
When dealing with img tags, it is possible to set the src attribute to a file name with a relative path and thereby quickly and easily load an image from the app's directory. I would like to use a similar method to retrieve a text file from the app's directory.
I have used TideSDK, but it is less lightweight. And I am aware of HTTP requests, but if I remember correctly only Firefox has taken kindly to my use of this for local file access (although accessing local images with src does not appear to be an issue). I am also aware of the FileReader object; however, my interface requires that I load a file based on the file name and not based on a file-browser selection as with <input type="file">.
Is there some way of accomplishing this type of file access, or am I stuck with the methods mentioned above?
The browser will not permit you to access files like that but you can make javascript files instead of text files like this:
text1.js:
document.write('This is the text I want to show in here.'); //this is the content of the javascript file
Now call it anywhere you like:
<script type="text/javascript" src="text1.js"></script>
There are too many security issues (restrictions) within browsers making many local web-apps impossible to implement so my solution to a similar problem was to move out of browsers and into node-webkit which combines Chromium + Node.js + your scripts, into an executable with full disk I/O.
http://nwjs.io/
[edit] I'm sorry I thought you wanted to do this with TideSDK, I'll let my answer in case you want to give another try to TideSDK [/edit]
I'm not sure if it's what you're looking for but I will try to explain my case.
I've an application which allow the user to save the state of his progress. To do this, I allow him to select a folder, enter a filename and write this file. When the user open the app, he can open the saved file, and get back his progress. So I assume this enhancement is similar of what you are looking for.
In my case, I use the native File Select to allow the user to select a specific save (I'm using CoffeeScript) :
Ti.UI.currentWindow.openFileChooserDialog(_fileSelected, {
title: 'Select a file'
path: Ti.Filesystem.getDocumentsDirectory().nativePath()
multiple: false
})
(related doc http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.UI.UserWindow-method-openFileChooserDialog)
When this step is done I will open the selected file :
if !filePath?
fileToLoad = Ti.Filesystem.getFile(scope.fileSelected.nativePath())
else
fileToLoad = Ti.Filesystem.getFile(filePath)
data = Ti.JSON.parse(fileToLoad.read())
(related doc http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.Filesystem)
Please note that those snippets are copy/paste from my project and they will not work without the rest of my code but I think it's enough to illustrate you how I manage to open a file, and read his content.
In this case I'm using Ti.JSON.parse because there is only javascript object in these files but in your case you can just get the content. The openFileChooserDialog isn't mandatory, if you already know the file name, or if you get it from another way you can use Ti.Filesystem in your own way.

Categories