如果url包含#,则不执行脚本

时间:2011-09-12 07:53:24

标签: javascript jquery

我想只在网址不包含#

时才执行下面的脚本

因此,如果url = http://win-e98sopqc735/Previsions/Lists/Prvisions/ViewParDateJour.aspx#ServerFilter=FilterField1

,请不要执行脚本

但是如果url =则执行 http://win-e98sopqc735/Previsions/Lists/Prvisions/ViewParDateJour.aspx

function addDays(dateObj, numDays) {
            dateObj.setDate(dateObj.getDate() + numDays);
            return dateObj;
        }

        function dateToShortString(date) {
            var d = date.getDate();
            var days = (d < 10) ? '0' + d : d;
            var m = date.getMonth() + 1;
            var month = (m < 10) ? '0' + m : m;
            var year = date.getFullYear();
            var shortDateString = days + "/" + month + "/" + year;
            return shortDateString;
        }

        var now = new Date();
        var today = dateToShortString(now);
        var tomorrow = dateToShortString(addDays(now, 1));
        var nextWeek = dateToShortString(addDays(now, 8));


        var url = location.pathname;
        //Set today's date if url contains ViewParRubriqueJour.aspx and ViewParDateJour.aspx
        if (url.indexOf('ViewParRubriqueJour.aspx') >= 0 || url.indexOf('ViewParDateJour.aspx') >= 0) {
            jQuery("input[id*='ctl00_ctl00_ctl00']").val(today);
            jQuery("input[id*='ctl00_ctl01_ctl00']").val(today);
        }
        //Set tomorrow's date if url contains ViewParDateDemain.aspx and ViewParRubriqueDemain.aspx
        if (url.indexOf('ViewParDateDemain.aspx') >= 0 || url.indexOf('ViewParRubriqueDemain.aspx') >= 0) {
            jQuery("input[id*='ctl00_ctl00_ctl00']").val(tomorrow);
            jQuery("input[id*='ctl00_ctl01_ctl00']").val(tomorrow);
        }
        //Set 7 days date if url contains ViewParDate7.aspx and ViewParRubrique7.aspx
        if (url.indexOf('ViewParDate7.aspx') >= 0 || url.indexOf('ViewParRubrique7.aspx') >= 0) {
            jQuery("input[id*='ctl00_ctl00_ctl00']").val(tomorrow);
            jQuery("input[id*='ctl00_ctl01_ctl00']").val(nextWeek);
        }

3 个答案:

答案 0 :(得分:2)

检查window.location.hash,如果没有设置哈希,它将是#ServerFilter=FilterField1或空字符串。

请注意,只有在#标签后面有内容时才会有效。如果只有一个标签,则必须解析window.location

答案 1 :(得分:1)

以下代码查找网址中的任何哈希值。

if (!window.location.hash) {
    // there is no hash, so execute stuff
}

如果要查找特定哈希,请使用:

if (!window.location.hash == "specific-hash") {
    // execute stuff
}

答案 2 :(得分:1)

function addDays(dateObj, numDays) {

if(window.location.href.indexOf('#')) return; // this will stop the script

            dateObj.setDate(dateObj.getDate() + numDays);
            return dateObj;
        }

        function dateToShortString(date) {
            var d = date.getDate();
            var days = (d < 10) ? '0' + d : d;
            var m = date.getMonth() + 1;
            var month = (m < 10) ? '0' + m : m;
            var year = date.getFullYear();
            var shortDateString = days + "/" + month + "/" + year;
            return shortDateString;
        }
....
....