You must Sign In to post a response.
  • Category: JavaScript

    Javascript not firing when trying to control from new tab

    Below is my code which works through keyboard keys after page loads in the browser without using mouse now in addition to this what i need is when the user open new tab in browser the audio file that is getting controlled through keyboard keys in previous tab should get controlled i tried the below code but it is not working

    This is the code which works through keyboard keys after the page loads in browser


    <script type = "text/javascript">
    var audio = document.getElementById("player");

    window.addEventListener("keydown", playPauseKb, false);

    function playPauseKb(event) {
    var x = event.keyCode;
    console.log(x);

    if (x == 120) {
    audio.currentTime -= 5;//F9 Rewined
    }

    else if (x == 122) {
    //event.data.$.preventDefault();
    audio.currentTime += 5;//F11 forword
    }

    else if (x == 121) {
    if (audio.paused) {
    audio.play();//F10 paly/pause
    }
    else {
    audio.pause()
    }
    }

    }
    var count = 0;
    function Forward() {
    event.data.$.preventDefault();
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.4;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    event.data.$.preventDefault();
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    window.addEventListener("keydown", function (e) {
    // f6, page up, page down and arrow keys:
    if ([117,119,120,121, 122, 123, 113, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
    e.preventDefault();
    }
    }, false);
    </script>

    <script type = "text/javascript">
    $(document).ready(function () {
    CKEDITOR.on('instanceCreated', function (e) {
    e.editor.on('contentDom', function () {
    e.editor.document.on('keydown', function (event) {

    if (event.data.$.keyCode == 120) {
    event.data.$.preventDefault();
    audio.currentTime -= 5;//f10_Rewind
    }

    else if (event.data.$.keyCode == 122) {
    event.data.$.preventDefault();
    audio.currentTime += 5;//F11 farward
    }
    else if (event.data.$.keyCode == 121) {
    event.data.$.preventDefault();
    if (audio.paused) {
    audio.play();//f6_play and pause
    }
    else {
    audio.pause()
    }
    }
    });
    var count = 0;
    function Forward() {
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }

    });
    });
    });

    </script>

    To this code i need to use javascript to control from another tab with the same keyboard keys i tried the below code but it is not working


    $(document).ready(function(){
    $(window).blur(function(){
    var audio = document.getElementById("player");

    window.addEventListener("keydown", playPauseKb, false);

    function playPauseKb(event) {
    var x = event.keyCode;
    console.log(x);
    if (x == 120) {
    audio.currentTime -= 5;//F9 Rewined
    }

    else if (x == 122) {
    //event.data.$.preventDefault();
    audio.currentTime += 5;//F11 forword
    }

    else if (x == 121) {
    if (audio.paused) {
    audio.play();//F10 paly/pause
    }
    else {
    audio.pause()
    }
    }

    }
    var count = 0;
    function Forward() {
    event.data.$.preventDefault();
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.4;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    event.data.$.preventDefault();
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    window.addEventListener("keydown", function (e) {
    // f6, page up, page down and arrow keys:
    if ([117,119,120,121, 122, 123, 113, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
    e.preventDefault();
    }
    }, false);
    CKEDITOR.on('instanceCreated', function (e) {
    e.editor.on('contentDom', function () {
    e.editor.document.on('keydown', function (event) {
    if (event.data.$.keyCode == 120) {
    event.data.$.preventDefault();
    audio.currentTime -= 5;//f10_Rewind
    }

    else if (event.data.$.keyCode == 122) {
    event.data.$.preventDefault();
    audio.currentTime += 5;//F11 farward
    }
    else if (event.data.$.keyCode == 121) {
    event.data.$.preventDefault();
    if (audio.paused) {
    audio.play();//f6_play and pause
    }
    else {
    audio.pause()
    }
    }
    });
    var count = 0;
    function Forward() {
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }

    });
    });
    });


    });
    $(window).focus(function(){
    var audio = document.getElementById("player");

    window.addEventListener("keydown", playPauseKb, false);

    function playPauseKb(event) {
    var x = event.keyCode;
    console.log(x);

    //if (x == 120) {
    // audio.currentTime += 5;//F9 forword
    //}
    //else if (x == 121) {
    // audio.currentTime -= 5;//F10 Rewined
    //}
    //if (x == 118) {
    // audio.volume += 0.1;//up arrow
    //}
    //else if (x == 119) {
    // audio.volume -= 0.1;//down arrow
    //}
    //else if (x == 122) {
    // event.data.$.preventDefault();
    // Forward();//F11 Fast forword
    //}
    //else if (x == 123) {
    // event.data.$.preventDefault();
    // Slow();//F12 slow
    //}

    //else if (x == 117) {
    // if (audio.paused) {
    // audio.play();//F6 paly/pause
    // }
    // else {
    // audio.pause()
    // }
    //}
    if (x == 120) {
    audio.currentTime -= 5;//F9 Rewined
    }

    else if (x == 122) {
    //event.data.$.preventDefault();
    audio.currentTime += 5;//F11 forword
    }

    else if (x == 121) {
    if (audio.paused) {
    audio.play();//F10 paly/pause
    }
    else {
    audio.pause()
    }
    }

    }
    var count = 0;
    function Forward() {
    event.data.$.preventDefault();
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.4;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    event.data.$.preventDefault();
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    window.addEventListener("keydown", function (e) {
    // f6, page up, page down and arrow keys:
    if ([117,119,120,121, 122, 123, 113, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
    e.preventDefault();
    }
    }, false);
    CKEDITOR.on('instanceCreated', function (e) {
    e.editor.on('contentDom', function () {
    e.editor.document.on('keydown', function (event) {
    if (event.data.$.keyCode == 120) {
    event.data.$.preventDefault();
    audio.currentTime -= 5;//f10_Rewind
    }

    else if (event.data.$.keyCode == 122) {
    event.data.$.preventDefault();
    audio.currentTime += 5;//F11 farward
    }
    else if (event.data.$.keyCode == 121) {
    event.data.$.preventDefault();
    if (audio.paused) {
    audio.play();//f6_play and pause
    }
    else {
    audio.pause()
    }
    }
    });
    var count = 0;
    function Forward() {
    count++;

    if (count == 1) {
    audio.playbackRate = 1.2;
    }
    if (count == 2) {
    audio.playbackRate = 1.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }
    var count = 0;
    function Slow() {
    count++;
    if (count == 1) {
    audio.playbackRate = 0.9;
    }
    if (count == 2) {
    audio.playbackRate = 0.8;
    }
    if (count == 3) {
    audio.playbackRate = 1.0;
    count = 1;
    }
    }

    });
    });
    });
    });
    });
  • #767695
    Hai Kavitha,
    Have you tried without using
    $(window).blur(function(){ 
    as this might prevent the script to not fire again.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767723
    <script type="text/javascript">
    // Set the name of the hidden property and the change event for visibility
    var hidden, visibilityChange;
    if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
    hidden = "hidden";
    visibilityChange = "visibilitychange";
    } else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
    visibilityChange = "mozvisibilitychange";
    } else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
    } else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
    }

    var audio = document.getElementById("player");

    // If the page is hidden, pause the video;
    // if the page is shown, play the video
    function handleVisibilityChange() {
    if (document[hidden]) {
    audio.pause();
    } else {
    audio.play();
    }
    }

    // Warn if the browser doesn't support addEventListener or the Page Visibility API
    if (typeof document.addEventListener === "undefined" || typeof document[hidden] === "undefined") {
    alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
    } else {
    // Handle page visibility change
    document.addEventListener(visibilityChange, handleVisibilityChange, false);

    // When the video pauses, set the title.
    // This shows the paused
    audio.addEventListener("pause", function () {
    document.title = 'Paused';
    }, false);

    // When the video plays, set the title.
    audio.addEventListener("play", function () {
    document.title = 'Playing';
    }, false);

    }
    </script>



    Below is my code with which i am able to stop playing the audio file when i am leaving that tab and again when i return back to that tab i am able to play the audio but my requirement is to play and pause the audio from that new tab's that is i should control from new tab using F10 key how can i do this


Sign In to post your comments