image file paths

  • Thread starter William Starr Moake
  • Start date
W

William Starr Moake

Is there a script to convert image file paths from absolute to just
img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The
doImage execCommand produces an absolute file path, which then must be
shortened manually before the user uploads the page to his web host.
 
J

Jakob Lund Krarup

William Starr Moake said:
Is there a script to convert image file paths from absolute to just
img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The
doImage execCommand produces an absolute file path, which then must be
shortened manually before the user uploads the page to his web host.

Hi William
The path to the file will usually be interspersed with "/" or "\" signs to
show where the different folders are in the path.
E.g.: "graphics/myItems/image.gif"
You can split that URL using the String objects split() method, and then use
the last element in the resulting array:


//This function returns only the filename from a string containing a path
function getFileName(path)
{
var pathArray = path.split("/");
return pathArray[pathArray.length-1];
}

Try it out at: http://www.jake.dk/programmering/javascript/forWilliam

Kind regards - Jakob
 
W

William Starr Moake

William Starr Moake said:
Is there a script to convert image file paths from absolute to just
img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The
doImage execCommand produces an absolute file path, which then must be
shortened manually before the user uploads the page to his web host.

Hi William
The path to the file will usually be interspersed with "/" or "\" signs to
show where the different folders are in the path.
E.g.: "graphics/myItems/image.gif"
You can split that URL using the String objects split() method, and then use
the last element in the resulting array:


//This function returns only the filename from a string containing a path
function getFileName(path)
{
var pathArray = path.split("/");
return pathArray[pathArray.length-1];
}

Try it out at: http://www.jake.dk/programmering/javascript/forWilliam

Kind regards - Jakob
Thanks for trying, but I can't get the script to work.
From file://C:\Windows\Desktop\Images\logo.gif it returns:
C:\Windows\Desktop\Images\logo.gif, simply removing the file://
I tried varying the minus number, but nothing worked.
I need a script that will return img src="logo.gif" to be placed in
the page source code when an image is inserted. The IE doImage
function by itself returns the full image path, which is a nightmare
to deal with for the user of a WYSIWYG editor.
 
I

Ivo

Jakob Lund Krarup said:
"William Starr Moake" skrev
//This function returns only the filename from a string containing a path
function getFileName(path)
{
var pathArray = path.split("/");
return pathArray[pathArray.length-1];
}

Try it out at: http://www.jake.dk/programmering/javascript/forWilliam
Thanks for trying, but I can't get the script to work.
From file://C:\Windows\Desktop\Images\logo.gif it returns:
C:\Windows\Desktop\Images\logo.gif, simply removing the file://
I tried varying the minus number, but nothing worked.

Which number? The reason you're seeing this behaviour is because clearly the
function as present splits on slashes alone. If you change it to
var pathArray = path.split(/[\\\/]/);
it will split on slashes and backslashes. Or convert the MS Windows
backslashes to normal slashes first.
HTH
Ivo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top