ページのアドレスを取得する ページを移動する location.href
console.log(location.href); // 現在のページアドレスを取得する
setTimeout(() => {
location.href = "https://google.com"; // 指定ページに移動する
}, 3000);
// 現在URLがつぎのアドレスだった場合
https://rohikata-wd.com:8000/js/index.php?q=javascript#link
location.href:「https://rohikata-wd.com:8000/js/index.php?q=javascript#link」
location.protocol:「https:」
location.hostname:「rohikata-wd.com」
location.host:「rohikata-wd.com:8000」
location.port:「8000」
location.pathname:「/js/index.php」
location.search:「?q=javascript」
location.hash:#link
location.hashは「#link」
// https://test.com:8000?url=htts://test.com/nextpage
const url = new URL(location.href);
const redirectUrl = url.searchParams.get('url');
if(redirectUrl){
const redirectUrlObj = new URL(uredirectUrl,location.href);
// 同一オリジンの確認
if(redirectUrlObj.origin === location.origin){
location.href=redirectUrl;
}
}