R
R
Roman2011-08-13 20:26:52
JavaScript
Roman, 2011-08-13 20:26:52

Location.hash......?

In general, there is a link like:
http://site.ru#more:next

There is a thing that catches the hash change in the browser to jquery:

$(window).bind('hashchange', function() {<br>
     alert(location.hash);<br>
});


There is one more thing that changes the hash, let's say this is a link: And all together: pastebin.com/0VhPHjXM What do I need? Suppose we opened a document and followed a link, an alert occurs. We pass the second time by the link: nothing (that is, no event occurs). I had several options on how to make the hash change equal to the hash in the query string:
<a href='#more:next'>ссылка</a>



<a href='#more:next'>Ссылка</a> <!-- не работает --><br>
<a href='#more:next/'>Ссылка</a> <!-- не подходит --><br>
<a href='#more:next '>Ссылка</a> <!-- не работает в опере --><br>

Answer the question

In order to leave comments, you need to log in

7 answer(s)
B
Borro, 2011-08-15
@Borro

And that won't work?


$('a').click(function(){
     $(window).trigger('hashchange');
});

R
Riateche, 2011-08-14
@Riateche

If the hash to "#more:next" event occurs, immediately change the hash to a string representing the page address (for example, "#page/42"). Thus, the next time you click on the "#more:next" link, the hash change event will fire again.

A
Anatoly, 2011-08-13
@taliban


setTimeout(hashCheck, 100);
var oldHash = null;
function hashCheck()
{
    if( oldHash !== location.hash )
    {
        // хеш изменился
    }
    setTimeout(hashCheck, 100);
}

A
Alexander, 2011-08-13
@disc

If you click a second time on the link with the current hash, the event will not work, because the hash does not change.
Maybe we should handle the onclick event and call hashchange() in it?

W
Wott, 2011-08-13
@Wott

And who is stopping you from changing the "active" hash to something else by loading and / or triggering the hashchange event?
ideologically, browsers are right - hash is used for positioning within the page, and if we are already in place, then there is no point in twitching. but if hash is used cunningly asshole , then, accordingly, it is also necessary to provide it cunningly.

G
gaelpa, 2011-08-13
@gaelpa

Bind an event to links that change the hash?

D
dhalturin, 2014-10-13
@dhalturin

var _hash = {
    param: {},
    current: '',
    set: function(data)
    {
        if(_hash.current == data)
        {
            log('lock change hash');
            
            return;
        }

        window.location.href = '#' + data;
    },
    get: function()
    {
        var _href = window.location.href.split('#'); _hash.param = {};

        if(_href[1])
        {
            var _tmp = _href[1].split('&');

            for(i in _tmp)
            {
                _hash.param[_tmp[i].split('=')[0]] = _tmp[i].split('=')[1];
            }
        }

        _hash.current = _href[1];

        log('param changed: ', _hash.param);
    }
};

$(document).rady(function(){
    $('a:not(.nolink)').on('click', function (event)
    {
        event.preventDefault();

        _hash.set(this.href.replace(/^.*#/, ''));
    });

    $(window).bind('hashchange', function()
    {
        _hash.get();
    });

    _hash.get();
});

In _hash.get you parse the string as you please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question