将多个JS文件合并为一个

时间:2016-01-10 10:03:48

标签: javascript jquery

我在许多页面上有一个URL重定向JS代码,即Mon.html,Tue.html等,我想将它组合成一个文件,并仍然根据屏幕宽度将用户重定向到页面的移动版本例如

    public void Test() {
        using (HttpClient client = new HttpClient()) {

        client.BaseAddress = new Uri("http://testservice.cloudapp.net");
        var response = client.PostAsync("api/test?value=1234", new StringContent(string.Empty)).Result;
        var statusCode = response.StatusCode;
        var errorText = response.ReasonPhrase;

        // response.EnsureSuccessStatusCode(); will throw an exception if status code does not indicate success

        var responseContentAsString = response.Content.ReadAsStringAsync().Result;
        var responseContentAsBYtes = response.Content.ReadAsByteArrayAsync().Result;
    }

}

这意味着我有很多JS文件,各自有public async Task TestAsync() { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://testservice.cloudapp.net"); var response = await client.PostAsync("api/test?value=1234", new StringContent(string.Empty)); var statusCode = response.StatusCode; var errorText = response.ReasonPhrase; // response.EnsureSuccessStatusCode(); will throw an exception if status code does not indicate success var responseContentAsString = await response.Content.ReadAsStringAsync(); var responseContentAsBYtes = await response.Content.ReadAsByteArrayAsync(); } } 我试图让一个JS文件包含所有必要的重定向。

2 个答案:

答案 0 :(得分:1)

您可以将当前位置映射到重定向。

if(screen.width <= 800) {
    switch(window.location) {
        case firstLocation:
            window.location = firstRedirect;
        case secondLocation:
            window.location = secondRedirect;
       // other redirect cases
       default:
           throw new Error('no redirect assigned to current location');
    }
}

或者,如果重定向具有相同的模式,则可以根据该模式修改当前位置。这将更为可取,因为它不包含大量硬编码位置,因此您不必每次都手动更新它。

if (screen.width <= 800) {
    window.location = '../m/' + window.location;
}

答案 1 :(得分:0)

想出来。

var screen_width = screen.width;

$(function() {
  if ($('body').is('.index') && (screen_width <= 800)) {
    window.location = "m/index.html";
  }
});