470,638 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

image path conversion script

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[i][0],"<img
src='"+imgSrcArray[i][1])
}
var html = iView.document.documentElement.outerHTML;
iView.document.write(HTML);
iView.document.execCommand("SaveAs");
}
Jul 23 '05 #1
1 1827
William Starr Moake wrote:
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? <40**************@PointedEars.de>

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[i].src = imgs[i].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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael J. Astrauskas | last post: by
3 posts views Thread by dave | last post: by
7 posts views Thread by Scott Schluer | last post: by
4 posts views Thread by tshad | last post: by
4 posts views Thread by yatin | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.