调用字符串时出错:indexOf

Way*_*ief 2 javascript indexof

该行var ndx = url.indexOf( parameter );导致代码返回而不显示以下任一警报.显示网址的提示是正确的.

function setFrameSrc() {
    var url = document.location;
    if (null == url) {
        alert('Javascript Error: Null Object - document.location');
        return;
    }
    alert('URL = ' + url);

    var parameter = '?image=';
    var ndx = url.indexOf(parameter);
    if (ndx < 0) {
        alert('Parameter not found = ' + parameter);
        return;
    }
    alert('Index of ' + parameter + ' = ' + ndx);

    var frame = document.getElementById('pframe');
    if (null == frame) {
        alert('Javascript Error: Null Object - frame');
        return;
    }
    frame.src = url.substring(ndx);
}
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题?

ick*_*fay 5

document.location尽管它的出现,但不是一个字符串.你想要的document.location.href.

更好的是,您可以使用document.location.search,其中仅包括包含和之后的部分?(以及不包括在内的部分和之后#).