如何根据一些条件允许某些网址和阻止其他网址

时间:2012-11-08 06:52:15

标签: javascript

基本上我有一个可以使用的变量,它提供了某个图像的成员$ member_profile_image。我正在尝试检查该网址,然后在其中找到“默认”一词。我已经这样做了,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>

2 个答案:

答案 0 :(得分: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-您必须自己为indexOf实施Array(或使用polyfill)

同时假设它是您的域名并且不会更改,您可以检查网址路径,如下所示:

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');
}

答案 1 :(得分:-1)

<\ n>在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");
}