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)
这段代码有什么问题?
document.location尽管它的出现,但不是一个字符串.你想要的document.location.href.
更好的是,您可以使用document.location.search,其中仅包括包含和之后的部分?(以及不包括在内的部分和之后#).
| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |