IE makes extra GET-request on hash change

570 Views Asked by At

When I make an XMLHttpRequest, I also change window.location.hash.

For example, mysite.com/gallery/q#1 becomes mysite.com/gallery/q#2.

When this happens, IE8, as Fiddler and nginx logs show, makes this strange extra request to mysite.com/gallery/ (which is 404).

The page isn't reloading, it's like an XMLHttpRequest.

GET http://mysite.com/gallery/ HTTP/1.1
Accept: */*
Referer: http://mysite.com/gallery/q
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
Accept-Encoding: gzip, deflate
Host: mysite.com
Connection: Keep-Alive

Separately, hash change or Ajax-request won't trigger this extra one.

Another thing to note – the extra request occurs not on every Ajax-request. It happens seemingly randomly.

Can it be nginx misconfiguration? Or is it simply one of the many IE8 bugs?

Is there a workaround? I don't want this extra load.


Update

Here's the Ajax code ($ stands for jQuery):

var id = link.getAttribute('data-id')

var xhr = $.ajax({
    cache: false,
    url: '/stock-items',
    method: 'GET',
    data: { id: id },
    dataType: 'json'
})
xhr.success(function (data) {
    if (currentId === id) {
        toggleLoader(false)
        displayData(data)
    }                                                                                                                           
})

And hash manipulating code:

function setHash(link) {
    var index = $(link).index()
    globals.location.hash = index + 1
}

Also tried with the hash-symbol with the same result:

globals.location.hash = '#' + index + 1

The Ajax-request is on click on gallery image links:

links.on('click', function (e) {
    setHash(this)                                                                                       
    loadData(this)
    e.preventDefault()
})

I also tried these links to have the href attribute set to #1, #2 and so on in the HTML (and removed e.preventDefault()). So that the hash changes naturally. Nope, the extra request is made anyway.

1

There are 1 best solutions below

3
On BEST ANSWER

Is the visitor being redirected to the affected page from another? If so, it's likely to be a known IE bug.

Take a look at this similar question: javascript location.hash refreshing in IE