函数嵌套在if语句中

Gre*_*lan 3 javascript location if-statement function match

如果有人能帮助我,我会遇到以下代码的问题我会非常感激.基本上我在调用网址的页面上的几个页面上有几个链接.目前,/freeten/即使在窗口中打开了extraten页面,它也只在代码部分内调用该函数.

if(location.href.match('/extraten/')) {

    console.log(window.location.href);

    function downloadXM() {
        if(location.href.match('/en/')) {
            window.location.href = "http://core77.com/";
        }

        if(location.href.match('/fr/')) {
            window.location.href = "http://www.notcot.org/";
        }

        if(location.href.match('/de/')) {
            window.location.href = "http://www.spd.org/";
        }

        if(location.href.match('/it/')) {
            window.location.href = "http://sxsw.com/";
        }

    }
}

if(location.href.match('/freeten/')) {

    function downloadXM() {

        if(location.href.match('/en/')) {
            window.location.href = "http://www.wired.com/";
        }

        if(location.href.match('/fr/')) {
            window.location.href = "http://www.bbc.co.uk/";
        }

        if(location.href.match('/de/')) {
            window.location.href = "http://edition.cnn.com/";
        }

        if(location.href.match('/it/')) {
            window.location.href = "http://www.sky.com/";
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

MMM*_*MMM 6

不要以这种方式声明函数,而是尝试这样做:

var myFunc;

if (something) {
    myFunc = function () { ... }
} else if (something) {
    myFunc = function () { ... }
}
Run Code Online (Sandbox Code Playgroud)