Мобильное обнаружение Innerhtml в php-коде functions.php

В моем файле wordpress functions.php у меня есть следующий код:

function mia_on_load_script()
{
    // Not our page, do nothing
    if( !is_page( 'test' ) )
        return;
?>

    <script type="text/javascript">
function change1(){
  document.getElementById('mydiv').innerHTML = '<div id="1" style="margin-left: 0px; padding: 0px; float: left; width: 640px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/embed/******?wmode=direct&amp;autoplay=true" width="640" height="392" frameborder="no"></iframe></div><div id="2" style="margin-left: 0px; padding: 0px; float: left; width: 320px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/socialstream/******" width="320" height="392" frameborder="no"></iframe></div><div id="addthisc" style="margin:0px; text-align: center; background-color:#0A0A0C; paddding: 0px 0px 0px 0px; width: 640px; height: 100%;  border:0px;"><div class="addthis_native_toolbox"></div></div>';
}
</script>


    <?php   
    };        
add_action( 'wp_head', 'mia_on_load_script' );

?>

Html-код в тесте веб-страницы выглядит следующим образом:

<input type="button" onclick="change1()" value="change html in mydiv " />

Эта функция изменяет html в div: 'mydiv', нажав кнопку "изменить html в mydiv"

Вместо изменения html, нажав кнопку с помощью javascript, я хочу изменить его при загрузке веб-страницы, когда запрос делается из мобильного браузера, поэтому я решил использовать класс Mobile_Detect.php (https://github.com/serbanghita/Mobile-Detect)

моя предыдущая функция стала:

function mia_on_load_script()
{
    // Not our page, do nothing
    if( !is_page( 'test' ) )
        return;
?>

         <?php   
 require_once(TEMPLATEPATH . '/MyScript/Mobile_Detect.php');
 $detect = new Mobile_Detect; 
 if ( $detect->isMobile() ) {
???????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????
}

?>



    <?php   
    };        
add_action( 'wp_head', 'mia_on_load_script' );

?>

Итак, какой код я должен вставить вместо вопросительных знаков, чтобы изменить html вместо «document.getElementById('mydiv').innerHTML'? Спасибо


person grigione    schedule 22.03.2015    source источник


Ответы (1)


Используйте 1_:

function mia_on_load_script()
{
    if(is_page('test'))
    {
        require_once(TEMPLATEPATH . '/MyScript/Mobile_Detect.php');
        $detect = new Mobile_Detect; 
        if($detect->isMobile())
        {
?>

<script> <!-- No "type" attribute for HTML 5 -->
window.addEventListener('load', function()
{
    document.getElementById('mydiv').innerHTML = '<div id="1" style="margin-left: 0px; padding: 0px; float: left; width: 640px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/embed/******?wmode=direct&amp;autoplay=true" width="640" height="392" frameborder="no"></iframe></div><div id="2" style="margin-left: 0px; padding: 0px; float: left; width: 320px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/socialstream/******" width="320" height="392" frameborder="no"></iframe></div><div id="addthisc" style="margin:0px; text-align: center; background-color:#0A0A0C; paddding: 0px 0px 0px 0px; width: 640px; height: 100%;  border:0px;"><div class="addthis_native_toolbox"></div></div>';
});
</script>

<?php
        }
    }
}
add_action('wp_head', 'mia_on_load_script');
person Siguza    schedule 22.03.2015