2017-11-16 1 views
1

이 코드를 작성하는 가장 좋은 방법은 무엇입니까? 페이지에 URL을 추가하려면? Redirect = No를 추가하는 방법을 알고있는 사람들이 새 사이트 또는 "이동 한 페이지"로 리디렉션되도록하십시오. 예를 들어 GET URL 사용 사례를 찾는 방법을 아는 데 많은 도움이됩니다. 나는 noobie 자바 스크립트 코더 그래서 귀하의 도움을 많이 주셔서 감사합니다!자바 스크립트에서 리디렉션 : IF GET 매개 변수 키를 사용하여 페이지에 머물 수 있습니다. 다른 페이지로 리디렉션

옵션 # 1

  • 사용 사례 1 : oldsite.mysite.com : 새로운 사이트를 사용하는 경우
  • 사용 사례 2로 리디렉션 : oldsite.mysite.com?Redirect= 아니오 : 리디렉션하지 않고 페이지가 정상적으로로드됩니다.

옵션 2 (더 사용자 친화적 인 접근 방식)

  • 사용 사례 1 :이 oldsite.mysite.com :는 oldsite.mysite.com?Redirect=No :하지 않는 사이트가 새 사이트

  • 사용 사례 2 즉시 리디렉션 리디렉션 일반적으로 페이지가로드

    사용 사례 3
  • : oldsite.mysite.com/AnyOtherPage이 :로 리디렉션 "우리가했습니다 "페이지 이동 후"x "초 후에 새 사이트로 리디렉션됩니다.

  • 사용 사례 4 : oldsite.mysite.com/AnyOtherPage?Redirect=No :하지 않습니다 리디렉션 정상적으로 페이지가로드


<script> 

// Check if the GET url parameter of "redirect=NO" is appended to the url 
and redirect if there is no parameter defined. 


$(function() { 


    if ( ... //url equals oldsite.mysite.com ... ) 

     //GET redirect=NO so do nothing and stay here 

     window.location.href = "https://newsite.mysite.com"; 

    } 



    else if (... //url equals oldsite.mysite.com/anyOtherPage ... ) 

    //GET redirect=NO so do nothing and stay here 

    window.location.href = "https://oldsite.mysite.com/we-have-moved"; 

    } 

}); 

</script> 

또는 그 이상을 볼 것 이거?

<script> 

switch(expression) { 

    case n: 
     //GET url contains parameter "Redirect=NO" 
     break; 

    case n: 
     if (//url contains a child page name) { 

      window.location.href = "https://newsite.mysite.com/we-have-moved"; 

      } 

     break; 

    default: 
     window.location.href = "https://newsite.mysite.com"; 

} 

</script> 

답변

0

실제 코드에서 주석을 제거하십시오.

var sleep =(ms)=> new Promise((res, rej)=>{ 
 
     setTimeout(res, ms); 
 
}); 
 
var redirect = async (pathname, ms) => { 
 
    if(ms){ 
 
     await sleep(ms); 
 
     console.log('waiting for' + ms + '...'); 
 
    }else{ 
 
     console.log('immediate'); 
 
    } 
 
    
 
    console.log('http://newUrl.com' + pathname);//location.href = 'http://newUrl.com' + pathname 
 
} 
 

 
var main = (url, ms) => { 
 
    var u =new URL(url); 
 
    var sp = Array.from(u.searchParams.entries()); 
 
    sp.some((arr,i)=>arr[0] === 'Redirect' && arr[1]=== 'No')? '': u.pathname === '/' ? redirect(''+u.search,0) : redirect(u.pathname+u.search, ms); 
 
} 
 

 

 

 

 
main('http://oldsite.mysite.com?Redirect=No', 5000); 
 
main('http://oldsite.mysite.com/AnyOtherPage?Redirect=No', 5000); 
 

 
console.log('redirection cases'); 
 
main('http://oldsite.mysite.com/AnyOtherPage', 5000); 
 
main('http://oldsite.mysite.com', 5000);

관련 문제