-1

I found two javascripts that I would like to combine.

First one:

<script type="text/javascript">            
function checker(url) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        location.href = url;
    }
}
checker("http://check");
</script>

And what that one does is pass the refer in IE

The second one:

<script language="JavaScript">
if (document.referrer.indexOf('somesite.com') > -1)
location.href='http://promo.com/';
else
location.href='http://mysite.com';
</script>

And the code above checks on where to send the user based if they are refered from the site they come from. I was trying to combine them into a single script. So I thought this would work:

<script type="text/javascript">
if (document.referrer.indexOf('somesite.com') > -1)
function check(url) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        location.href = url;
    }
}
check("http://check");
else
location.href='http://mysite.com'
</script>

Any suggestion?

4

1 回答 1

1

您不能将函数定义放在if语句中。先放函数定义,再放语句if

function check(url) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        location.href = url;
    }
}
if (document.referrer.indexOf('somesite.com') > -1)
    check("http://check");
else
    location.href='http://mysite.com'
于 2013-03-31T01:56:01.313 回答