image path conversion script

  • Thread starter William Starr Moake
  • Start date
W

William Starr Moake

This is for a browser-based WYSIWYG editor to convert image file paths
from absolute to relative when the page under construction is saved.
From inside the save function I get error message: "Cannot assign to
function result." If I place just the HTML.replace part of the
conversion script inside the save function and the rest outside, the
error message is "imgSrcArray undefined." Can anyone figure out what
is wrong with this path conversion script? The save function works by
itself.

function saveFrame() {
imgSrcArray() = new Array
if(imgSrc.indexOf("\\") > -1)
{
slash = imgSrc.lastIndexOf("\\");
imgSrcArray[imgSrcArray.length][0] = imgSrc
imgSrcArray[imgSrcArray.length][1] = imgSrc.substring(slash + 1,
imgSrc.length);
}
if(imgSrc.indexOf("/") > -1)
{
slash = imgSrc.lastIndexOf("/");
imgSrcArray[imgSrcArray.length][0] = imgSrc
imgSrcArray[imgSrcArray.length][1] = imgSrc.substring(slash + 1,
imgSrc.length);
}
for (i=0; i<imgSrcArray.length; i++)
{
HTML = HTML.replace("<img src='"+imgSrcArray[0],"<img
src='"+imgSrcArray[1])
}
var html = iView.document.documentElement.outerHTML;
iView.document.write(HTML);
iView.document.execCommand("SaveAs");
}
 
T

Thomas 'PointedEars' Lahn

William said:
function saveFrame() {
imgSrcArray() = new Array

"()" is the call operator, used for function/method calls only which
is why you get the "cannot assign to function result" and "imgSrcArray
undefined" errors.

var imgSrcArray = new Array;
if(imgSrc.indexOf("\\") > -1)

You are using evil[tm] globals where not necessary.
{
slash = imgSrc.lastIndexOf("\\");

`slash' is defined global here. Use the `var' keyword instead.
imgSrcArray[imgSrcArray.length][0] = imgSrc

imgSrcArray exists. imgSrcArray[imgSrcArray.length] exists, as it
is always equivalent to imgSrcArray[0] here (since you reinitialize
imgSrcArray every time the method is called, it contains no elements
on this line). But imgSrcArray[imgSrcArray.length][0] as well as the
equivalent imgSrcArray[0][0] do not exist. Reason: There is no object
to which to add the property `0'. Solution: Create the object (array
element) first, assign the reference to it to imgSrcArray[...] and then
add the property to it:

imgSrcArray[imgSrcArray.length] = new Object(); // or Array

// note that we added an element previously
// vvv
imgSrcArray[imgSrcArray.length - 1][0] = imgSrc;
imgSrcArray[imgSrcArray.length][1] = imgSrc.substring(slash + 1,
imgSrc.length);

The same here. imgSrcArray[imgSrcArray.length] has
the value `undefined' until you assign a value to it.
[...]
var html = iView.document.documentElement.outerHTML;
iView.document.write(HTML);
iView.document.execCommand("SaveAs");
}

As for the rest, your algorithm can certainly be improved. Why
haven't you considered mine? <[email protected]>

There is no need for an array anyway and there is no need to replace
the whole source only for image path conversion. Not only this is
inefficient, you are risking undesired side effects. Since it seems
you code for IE, you *could* use

function abs2rel(d)
{
if (d && typeof d.images != "undefined")
{
for (var i = 0, imgs = d.images, len = imgs.length;
i < len;
i++)
{
imgs.src = imgs.src.replace(/\\/g, "/").replace(/.*\//, "");
}
}
}

abs2rel(iView.document);

which does what your code does.

But note that neither your algorithm nor my second one will successfully
convert absolute to relative paths. For that, stripping the directory path
is not enough, you need to determine what directory is the local document
root first and then strip it as prefix, as I already explained when posted
my first algorithm (see the above message-id).


HTH

PointedEars
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top