0

基本上我有一个变量可以使用它提供某个图像 $member_profile_image 的 url。我正在尝试检查该网址,然后在其中找到“默认”一词。我已经这样做了,indexOf 显示为 22。这让我知道有人没有将照片上传到他们的个人资料中。如果他们没有上传个人资料照片,我希望他们只能访问我存储在数组 myurls 中的链接。因此,如果上传 indexOf 确实为 22,并且它们位于存储在 myurls 中的其中一个页面上,我根本不希望发生任何事情,但是如果他们尝试转到任何其他页面,我希望将其重定向到该页面上传一张照片。我对这一切都很陌生,所以我可能离这里很远。到目前为止,我唯一能做的就是重定向到上传照片的页面,但它会一遍又一遍地重新加载页面。下面的代码:

<script>
var image = "$member_profile_image"
var upload = image.indexOf("Default");
var myurls = new Array(3);
myurls[0] = "http://websiteforyou.spruz.com/?page=login&cmd=c&id=7394750B-F821-48B6-B6AC-1508D1932390&c=eb3hb4el98";
myurls[1] = "http://www.websiteforyou.spruz.com/?page=login&cmd=confirm";
myurls[2] = "http://www.websiteforyou.spruz.com";
myurls[3] = "http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal";
if(myurls[0,1,2] = window.location && upload == "22")
alert("have a great day");
else
alert("You have to upload a profile image to participate on this site");
window.location.replace("http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal");
</script>
4

2 回答 2

1

我不完全明白你在做什么,所以我做了很多假设!一般来说,你最大的错误是myurls[0,1,2,3] = window.location因为myurls[0,1,2,3]是无效的javascript并且=不比较而是分配。

我已经举了一个例子来说明我认为代码应该是什么,我只是希望我是对的。

var image   = <?php echo json_encode($member_profile_image) ?>,
    upload  = image.indexOf('Default') === 22,
    myurls  = [
        'http://websiteforyou.spruz.com/?page=login&cmd=c&id=7394750B-F821-48B6-B6AC-1508D1932390&c=eb3hb4el98',
        'http://www.websiteforyou.spruz.com/?page=login&cmd=confirm',
        'http://www.websiteforyou.spruz.com',
        'http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal'
    ];

if (myurls.indexOf(window.location) > -1 && upload) {
    alert('have a great day');
}
else {
    alert('You have to upload a profile image to participate on this site');
    window.location.replace('http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal');
}

如果使用 IE8- 你必须自己实现indexOfArray或使用 polyfill)

还假设它是您的域并且它没有改变,您可以检查 url 路径,如下所示:

var image   = <?php echo json_encode($member_profile_image) ?>,
    upload  = image.indexOf('Default') === 22,
    myurls  = [
        '/?page=login&cmd=c&id=7394750B-F821-48B6-B6AC-1508D1932390&c=eb3hb4el98',
        '/?page=settings&cmd=personal',
        '/?page=login&cmd=confirm',
        ''
    ];

if (myurls.indexOf(window.location.pathname) > -1 && upload) {
    alert('have a great day');
}
else {
    alert('You have to upload a profile image to participate on this site');
    window.location.replace('/member/?page=settings&cmd=personal');
}
于 2012-11-08T14:42:11.357 回答
-1

在 else 语句之后,您需要包装警报并将位置替换为 { } - 就目前而言,无论 if 检查的结果如何,都会运行 window.location 行。

也就是说,改变

if(myurls[0,1,2,3] = window.location && upload == "22")
alert("have a great day");
else
alert("You have to upload a profile image to participate on this site");
window.location.replace("http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal");

对此:

if(myurls[0,1,2,3] = window.location && upload == "22")
  alert("have a great day");
else
{
  alert("You have to upload a profile image to participate on this site");
  window.location.replace("http://www.websiteforyou.spruz.com/member/?page=settings&cmd=personal");
}
于 2012-11-08T07:55:20.630 回答