実現したいこと,試したこと
javascript
1$(function () { 2 var ua = navigator.userAgent; 3 if ((ua.indexOf('iPhone') > 0 || ua.indexOf('Android') > 0) && ua.indexOf('Mobile') > 0) { 4 // スマートフォン用処理 5 var Id_01 = mobile/activity/xxxxx?event_id=01; 6 var Id_02 = mobile/activity/xxxxx?event_id=02; 7 } else if (ua.indexOf('iPad') > 0 || ua.indexOf('Android') > 0) { 8 // タブレット用の処理 9 var Id_01 = mobile/activity/xxxxx?event_id=01; 10 var Id_02 = mobile/activity/xxxxx?event_id=02; 11 } else { 12 // それ以外PC用 13 var Id_01 = schedule/xxxxx?event_id=01; 14 var Id_02 = schedule/xxxxx?event_id=02; 15 } 16 }); 17 })
html
1<a href="javascript:window.location.href='https://www.xxxxxx.com/'+Id_01;" target="_blank"></a> 2<a href="javascript:window.location.href='https://www.xxxxxx.com/'+Id_02;" target="_blank"></a>
・モバイル&タブレット
https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=01
https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=02
・PCの時
https://www.xxxxxx.com/schedule/xxxxx?event_id=01
https://www.xxxxxx.com/schedule/xxxxx?event_id=02
上記のように、
①ページ読み込み時にモバイルかPCを判別し、②それぞれIDの付いたリンクを指定したいです。
一番上に書いたコードで試してみましたが、動きませんでした…。
このIDは「id=03」「id=04」...と増えていく予定です。
参考
上記を参考にいたしました。
何かもっと効率の良い方法がありましたらご教示ください。
よろしくお願いいたします。
### 実現方法
JavaScript
1$(function(){ 2 var ua = navigator.userAgent; 3 if ((ua.indexOf('iPhone') > 0 || ua.indexOf('Android') > 0) && ua.indexOf('Mobile') > 0){ 4 $('.id=01').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=01'); 5 $('.id=02').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=02'); 6 } else if (ua.indexOf('iPad') > 0 || ua.indexOf('Android') > 0){ 7 $('.id=01').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=01'); 8 $('.id=02').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=02'); 9 } else { 10 $('.id=01').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=01'); 11 $('.id=02').attr('href','https://www.xxxxxx.com/mobile/activity/xxxxx?event_id=02'); 12 } 13 });
この方法で思っていました動きは実現しました。
効率の良い方法、これから勉強したいと思います。
回答2件
あなたの回答
tips
プレビュー