Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

javascript - window.location not working correctly in IE7/8

I have a div which has an a inside it. I want to click on the div and follow the same url as the a. This is the HTML:

<div class="overview white getchildurl">
    <p class="w70">
    05-02-2011
    </p>
    <a class="button" href="details/">Details</a>
    <span class="clear"></span>
</div>

and I have this script:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        window.location = href;
    });
});

This works in Firefox 3.6, Chrome, Safari, but not in IE7 and IE8. It reloads the page, but stays on the same page. I checked the var href, it has the right url, but doesn't go there. What am I doing wrong?

Thanks.

EDIT: Making it an absolute URL made it work. This is the new script:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        var host = window.location.hostname;
        window.location = 'http://' + host + '/' + href;
    });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As I mentioned in my comment, I'm not 100% sure about it (how IE7+8 handle the .location object), but after all it's just an object.

Try to explicitly set the href property within the location object:

window.location.href = href;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...