简体   繁体   中英

Website redirecting to homepage after clicking on a page jump

There is a page on my ModX website that includes a couple of page jump links at the top for a few articles that are further down the page. This is the HTML for each of them:

<a class="jump" href="#article1">Article 1</a><br />
<a class="jump" href="#article2">Article 2</a><br />

<!-- and so on... -->

And before, the links were not working at all in IE 11 so I'm using the JavaScript here to check if the visitor is using IE and to perform the page jump:

var url = 'http://mywebsite/mywebpage';
$('a.jump').on('click', function(evt) {
    evt.preventDefault();
    var useragent = window.navigator.userAgent;
    if (useragent.indexOf("Trident") > -1 || useragent.indexOf("Edge") > -1) { // IE, Edge
        var href = $(this).attr('href');
        window.location.assign(url + href);
    } else { // anything else
        var href = $(this).attr('href');    
        href = href.substring(1, href.length);      
        window.location.assign('other/url#' + href); 
    }
});

I'm not sure if this is the best way to check for IE, but for now it seems to be working.

This works just fine in Chrome and Firefox, but if I click on one of the page jumps in IE it jumps correctly, but after a second passes it redirects to the homepage of my website.

I am using ModX Revolution 2.5.7.

EDIT: Microsoft Edge seems to redirect straight to the homepage. EDIT 2: Updated JS code to check for Edge properly.

也许evt.preventDefault()就在click函数中?

There is a page on my ModX website that includes a couple of page jump links at the top for a few articles that are further down the page.

I don't quite understand what you want to achieve. If you want visitors to actually open those links and read them, why not to just insert real links and make this basic html thing - browsing links - work?

If those articles are already present on the page (entirely or partly) as secondary content somewhere below the main article and you want just to draw visitor's attention by "jumping" to them without opening new tabs or loading another page, just in addition to <a href='#article1'></a> use <a name="#article1"></a> to indicate that place on your current page where the visitor should be scrolled to.

There are several issues with the piece of code you gave too.

First off, the way you check IE/Edge versions. As it has been pointed out in this answer , "Trident" is not a part of Edge user agent string nowadays. So, in case you want to target Edge, your condition isn't even triggered.

Also you don't need this: (url + href).toString() since both variables are already of type string. Just url + href .

Moreover, it feels as if there is another script on the page that handles clicks on links. I think we need the link to your page in question to check what is going on there. So, if you don't mind sharing the link, we could check more in depth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM