2

i would like to execute the script below only if the url doesn't contains #

So don't execute script if url = http://win-e98sopqc735/Previsions/Lists/Prvisions/ViewParDateJour.aspx#ServerFilter=FilterField1

but execute if 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);
        }
user472285
  • 2,604
  • 5
  • 37
  • 55

3 Answers3

2

Check for window.location.hash,it will be #ServerFilter=FilterField1 or an empty string in case no hash is set.

Note that this will only work if there's something after the hashtag (#). If there's only a hashtag alone, you will have to parse window.location.

thwd
  • 23,956
  • 8
  • 74
  • 108
1

The following code looks for any hash in the url.

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

If you want to look for a specific hash, use:

if (!window.location.hash == "specific-hash") {
    // execute stuff
}
Chad Levy
  • 10,032
  • 7
  • 41
  • 69
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;
        }
....
....
Walialu
  • 4,096
  • 2
  • 27
  • 29