jQuery:当 url 中存在特定哈希值时触发函数...?

发布于 2024-12-06 21:39:51 字数 146 浏览 1 评论 0原文

我希望在加载带有 url 中特定哈希值的页面时执行一个函数。

因此,如果我访问 www.mypage.com/hello.html#functionplease ,那么代码就会执行。

有人能指出我正确的方向吗?

任何建议表示赞赏!

I'm looking to execute a function when the a page is loaded with a certain hash in the url.

So if I visit www.mypage.com/hello.html#functionplease then the code will execute.

Can anyone point me in the right direction?

Any advice appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-12-13 21:39:51
var h = location.hash.substr(1);
if (h == 'functionplease')
    functionplease();

substr(1) 删除哈希中的#。

功能很多?没问题!

switch(location.hash.substr(1)) {
    case 'buy': buy(); break;
    case 'sell': sell(); break;
    case 'other': other(); break;
}
var h = location.hash.substr(1);
if (h == 'functionplease')
    functionplease();

substr(1) remove the # in hash.

Many functions? No problem!

switch(location.hash.substr(1)) {
    case 'buy': buy(); break;
    case 'sell': sell(); break;
    case 'other': other(); break;
}
如痴如狂 2024-12-13 21:39:51

以下内容应该有效:

<script type="text/javascript">
    //check if hash tag exists in the URL
    if(window.location.hash)
    {
        //set the value as a variable, and remove the #
        var hash_value = window.location.hash.replace('#', '');

        //check value of hash
        if (hash_value == 'functionplease'){
           // execute code
        }  
    }
</script>

希望这会有所帮助!

The following should work:

<script type="text/javascript">
    //check if hash tag exists in the URL
    if(window.location.hash)
    {
        //set the value as a variable, and remove the #
        var hash_value = window.location.hash.replace('#', '');

        //check value of hash
        if (hash_value == 'functionplease'){
           // execute code
        }  
    }
</script>

Hope this helps!

绅士风度i 2024-12-13 21:39:51
var pathname = window.location.pathname; 
if(pathname.indexOf("#") != -1){
    //call your function
}
var pathname = window.location.pathname; 
if(pathname.indexOf("#") != -1){
    //call your function
}
雨轻弹 2024-12-13 21:39:51

尝试:

<script type="text/javascript">
    window.onload = function () {
        setInterval(pollHash, 100);
    }


    var lasthash = '';
    function pollHash() {
        if (window.location.hash == lasthash) { return; }

        lasthash = window.location.hash;
        console.log('hash is changed! Current hash is: ' + lasthash);

        switch (lasthash) {
            case '???' :
                /* do something */
                break;
            default:
                /* do nothing */
                break;
        }
    }

</script>

看看:http://ajaxpatterns.org/Unique_URLs#Unique_URL_Sum_Demo

try:

<script type="text/javascript">
    window.onload = function () {
        setInterval(pollHash, 100);
    }


    var lasthash = '';
    function pollHash() {
        if (window.location.hash == lasthash) { return; }

        lasthash = window.location.hash;
        console.log('hash is changed! Current hash is: ' + lasthash);

        switch (lasthash) {
            case '???' :
                /* do something */
                break;
            default:
                /* do nothing */
                break;
        }
    }

</script>

have a look to: http://ajaxpatterns.org/Unique_URLs#Unique_URL_Sum_Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文