Hey guys I need some help
Im trying to change the src of an image tag but when change it using my code it adds the word file to the start of my string.
The final result is file:///C:/xampp/....
instead of just C:/xampp/....
Here is my code
for(var i = 0; i < images.length; i++) {
alert(images[i].src);
srcList.push(images[i].src);
var index = srcList[i].search("JavaBridge");
tempPath.push(srcList[i].slice(index-1, srcList[i].length));
finalPath.push("C:/xampp/tomcat/webapps"+tempPath[i]);
document.getElementById(images[i].id).src = " "+finalPath[i];
alert(images[i].src);
}
Thanks in Advance
For file manager the physical path is C:/xampp/.... but browser cannot use that directly. So, this is a default action and you cannot override it. Try to open a file with browser and see the url .. you will find file:/// appended before the path.
When you change this path to url.. the file:/// will not come.
One quick workaround is to use a server and load files using server.. You can easily setup server using apache (Xampp, Wamp) or Node (Gulp Task)
Related
I'm trying to find the absolute path of my javascript file (not the URL). Instead of hardcoding absolutely paths, I'd prefer using relative paths. For example:
/static/js/index.js
/static/config/1.json
/static/config/2.json
If I can get the absolute path of index.js, then I'm only ../config/ away from accessing either of the two json files.
Searching SO and the internet I either get suggestions for finding the URL, which work but won't solve my problem. Or for file paths, using windows.location.pathname but any permutation of it that I try either returns an empty string, or /.
var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/');
console.log("Curr dir: " + currentDirectory);
(Returns empty string)
var location = window.location.pathname;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
console.log(" dirPath: " + directoryPath);
(Returns a /)
What I'm hoping for is something like:
var absolute_path = window.function.absolute.path()
Which would return say: /u/user/develop/project/server/static/js/index.js
From which I could: (pseudo code again)
var locations = absolute_path.split("js");
# location[0] = "/u/user/develop/project/server/static"
# location[1] = "js/index.js"
var config_file_locations = locations[0] + "config/";
var absolute_path_json1 = config_file_locations + "1.json"
-- EDIT --
Ok, looking at window.location it returns the URL so clearly that's not an answer here.
/static/js/index.js is an absolute path. You can tell because it starts with a /, which takes it back to the root of the web site.
There is no automatic way for a browser to tell anything about how a web server determined how it generated the content for a given URL.
All a browser knows is that it asked the server for /static/js/index.js and the server responded with some JavaScript.
The server might have read a static file, and that file might be in a directory called js and that directory might be a subdirectory of one called static … but then the server might have taken the whole URL, used it in a database query, and pulled the results from a database … or it might have proxied the request to another HTTP server on another computer on another continent.
If you want your client-side JS to know anything about the structure of the filesystem on the HTTP server, then you need to give it that information somehow. It can't get it from the browser.
An HTML page handed off to me which originally have a image path which look something like this...
<img src="img/foo.jpg"
simply need to be changed to a new path.
I have a loop which goes through the collection of images:
container_images = container.getElementsByTagName('img');
imgSrc = /img\//gi,
for (var i = 0; i < container_images.length; i++) {
var images = container_images[i];
if (images.src.indexOf('img') !== -1) {
images.src = images.src.replace(imgSrc, 'new/path/here/');
}
}
Now this works perfectly locally however when I run this on my company's QAF server, it appears the server is adding a dev path:
<img src="http://ryelxusecqcm1.rye.com:8080/us-home/tools/img/foo.gif">
So is there a method other than .replace which can explicitly wipe out and the old path and put my new path? i.e. 'new/path/here/'
The problem may be because you are using a "relative" path in your code (i.e. a path that does not begin with "/"):
images.src = images.src.replace(imgSrc, 'new/path/here/');
Because you are using a relative path, your browser is prepending your path with the server's URL.
Try this and see if it helps.:
images.src = images.src.replace(imgSrc, '/new/path/here/');
(Note the leading "/" in '/new/path/here/')
I found such a simple but elegant solution. Instead of testing for a string with indexOf (which would've worked if I didn't have two different environments), I went for a solution which is more direct.
images.src = '/dam/avon-us/landing-pages/rep-news/' + images.src.split('/').pop();
This first coverts the image path/string to a collection of items in an array, delineated by the forward slash. ['current', 'path', 'foo.jpg'] Then using the pop method return just the last item in the array, which is the file name & extension. foo.jpg and then we simply prepend the path we want the image to have!
Want to say thanks for all the folks who tried to help me!
I have to crop all open psd files and save as jpg.
To speed up the workflow I am using a photoshop script that allows me to save the files one by one with specific jpg options.
I would like to enhanced the script by including a save all open files feature.
What would be the simples way to add this functionality to the script using javascript?
This is the structure of the script
//flatten image
//declare variable myJPEGOptions
//input jpg options
//save jpg to folder path
//close the document
The easiest way is to use Automate. You don't need to included anything special.
Write your script as you would normally with the intention of processing just one file.(which will flatten, process save and then close the document) as normal.
Then access your script in batch mode (File > Automate > Batch)
Select the your script and a folder with all your documents in.
Or if you want to modify your script you can:
// get all the files to process
var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
var fileList = inFolder.getFiles();
}
// and then loop over the file list
for (var i = 0; i < fileList.length; i++)
{
var doc = open(fileList[i]);
// process files here
// close files here
}
Just bare in mind that Photoshop will try to open any file in that list including things that it can't like text files or thumbnails thumbs.db :)
I am loading all images from a folder from local disk and displaying using javascript.
I have given a button to open a local disk and choose files.
But I want to dynamically load images from a particular path on DOMready/onload instead of loading though a file select.
Here is link to what I have tried: http://fiddle.jshell.net/rrYxn/
Please please help.
It is not possible to load file arbitrary from any users local disk.
if possible , when you open Facebook.. it can upload all the photos in you computer with out asking you ans post it :). so simply no.
what you may need is to load some file from server (not local drive/path).
you can define th files to want to load like
var files = [
'http://localhost/myproject/image1.png',
'http://localhost/myproject/image2.png',
'http://localhost/myproject/image3.png'
]
later the localhost will change to the domain name.
and use some script function to load these images
function loadImages(){
for(var i=0; i< files .length; i++) {
var img = new Image();
img.src = files[i];
document.getElementById('wrapper').appendChild(img);
}
}
how can i append data to a file using javascript?
i tried to use this code, but i got an error:
var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();
the error is on var fso = new ActiveXObject("Scripting.FileSystemOject");, written: Error: Automation server can't create object
is there any other way to append the file using javascript or the way to fix this? thanks :)
EDIT:
i have doing what's written on this, and it still not working :/
I just realized these in your code:
var fileObject = fso.OpenTextFile(filepath, 8,true);
You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.
var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);
OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.
EDIT
Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).
<hta:application
applicationName="MyApp"
id="myapp"
singleInstance="yes"
/>
Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.
EDIT II
In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...
var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');
And don't forget what I've said above about using absolute paths...
The 8 in the OpenTextFile function specify that you want to append to the file. Your problem comes from the security restriction of your browser. To make it work you'll have to lower the security level, which is not really recommended.
The error is thrown because there are security restrictions which donot allow the activex to run. change your security settings to allow the activex if your using internet explorer (which i think you are).
This might be useful http://windows.microsoft.com/en-US/windows/help/genuine/ie-activex
Cheers
EDIT: i have doing what's written on this, and it still not working :/
* try Restarting your browser
As pointed out in this comment
Javascript: how to append data to a file
the cause of the error Error: Automation server can't create object is the typo in the progid passed to ActiveXObject: Oject instead of Object:
var fso = new ActiveXObject("Scripting.FileSystemOject");
there is a missing b!