2017-11-16 4 views
1

このコードを書くにはどうすればよいでしょうか? 「Redirect = No」を追加する方法を知っている人が、URLをページに追加して表示する方法を知っていれば、その人は新しいサイトまたは「移動済み」ページにリダイレクトされます。例は、GET urlユースケースを検出する方法を知っていることに非常に役立ちます。私はあなたの助けが大いに評価されるようにnoobie javascriptコーダーです!javascriptでリダイレクト: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);

関連する問題