如果url包含#,则不执行脚本 [英] don't execute script if url contains #

查看:81
本文介绍了如果url包含#,则不执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只在网址不包含#

时执行下面的脚本所以如果url = http://win-e98sopqc735/Previsions/Lists/Prvisions/ViewParDateJour.aspx#ServerFilter=FilterField1

So don't execute script if 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);
        }


推荐答案

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

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
}

这篇关于如果url包含#,则不执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆