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

Categories

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

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

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...