문제

I have a window.location.href redirect in the <head> of a page. which happens when a certain condition is met and the user is redirected to a similar page, which uses the same header and footer templates (so the same JS code is there again - this may be important to possible solutions)

Sometimes, for a split second, just before the physical redirect kicks in, I see a bunch of code dumped in the browser window. This happens in about 1/20 times for me. For some other people I've asked, it happens more often.

Is this the rest of the JS on the page being dumped there as it breaks out before being fully parsed or...? I imagine there is a couple of hundred milliseconds of time in which the browser continues parsing the rest of the page after the redirect line. How would I prevent this code from showing on the page?

도움이 되었습니까?

해결책

If you insist on keeping the redirect on the client side - You can set the body to display:none , and then in the else branch of your conditional in JavaScript - show it:

CSS

body{
    display:none;
}

JS:

if(someCondition){
    location.hred = "http://othersite";
} else {
    document.body.style.display = "block";
}

Note that this has the disadvantage of showing a blank page to people who don't have JS enabled, so you might want to do it for a less 'drastic' tag than <body>.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top