image path conversion script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Starr Moake

    #1

    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 "imgSrcArra y 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.index Of("\\") > -1)
    {
    slash = imgSrc.lastInde xOf("\\");
    imgSrcArray[imgSrcArray.len gth][0] = imgSrc
    imgSrcArray[imgSrcArray.len gth][1] = imgSrc.substrin g(slash + 1,
    imgSrc.length);
    }
    if(imgSrc.index Of("/") > -1)
    {
    slash = imgSrc.lastInde xOf("/");
    imgSrcArray[imgSrcArray.len gth][0] = imgSrc
    imgSrcArray[imgSrcArray.len gth][1] = imgSrc.substrin g(slash + 1,
    imgSrc.length);
    }
    for (i=0; i<imgSrcArray.l ength; i++)
    {
    HTML = HTML.replace("< img src='"+imgSrcAr ray[i][0],"<img
    src='"+imgSrcAr ray[i][1])
    }
    var html = iView.document. documentElement .outerHTML;
    iView.document. write(HTML);
    iView.document. execCommand("Sa veAs");
    }
  • Thomas 'PointedEars' Lahn

    #2
    Re: image path conversion script

    William Starr Moake wrote:
    [color=blue]
    > function saveFrame() {
    > imgSrcArray() = new Array[/color]

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

    var imgSrcArray = new Array;
    [color=blue]
    > if(imgSrc.index Of("\\") > -1)[/color]

    You are using evil[tm] globals where not necessary.
    [color=blue]
    > {
    > slash = imgSrc.lastInde xOf("\\");[/color]

    `slash' is defined global here. Use the `var' keyword instead.
    [color=blue]
    > imgSrcArray[imgSrcArray.len gth][0] = imgSrc[/color]

    imgSrcArray exists. imgSrcArray[imgSrcArray.len gth] 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.len gth][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.len gth] = new Object(); // or Array

    // note that we added an element previously
    // vvv
    imgSrcArray[imgSrcArray.len gth - 1][0] = imgSrc;
    [color=blue]
    > imgSrcArray[imgSrcArray.len gth][1] = imgSrc.substrin g(slash + 1,
    > imgSrc.length);[/color]

    The same here. imgSrcArray[imgSrcArray.len gth] has
    the value `undefined' until you assign a value to it.
    [color=blue]
    > [...]
    > var html = iView.document. documentElement .outerHTML;
    > iView.document. write(HTML);
    > iView.document. execCommand("Sa veAs");
    > }[/color]

    As for the rest, your algorithm can certainly be improved. Why
    haven't you considered mine? <40958A3B.90706 00@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.d ocument);

    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

    Comment

    Working...