最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Enable and disable window.location.href - Stack Overflow

programmeradmin2浏览0评论

Recently, YouTube introduced the new Autoplay feature and I got jealous.

I implemented it on my tube site. My site uses jwplayer as a video player, it has a very beautiful and easy API where a function there is called onComplete, where it will execute my javascript queries at the end of the video.

What I did is this:

onComplete:function() {
//Video has ended, now we redirect to the new video
window.location.href = "//www.mysite/video?id=33412";
}

So, using this, my site will successfully redirect to a new video, and so on. But, what if the user turns off the "Autoplay" feature as shown in the image. How can I disable the window.location.href?

Other stackoverflow answers said:

You can't change the window.location prototype, because this a "native property" of window and it is not configurable.

What would be the solution in my case? A solution using Jquery is okay for me too.

Recently, YouTube introduced the new Autoplay feature and I got jealous.

I implemented it on my tube site. My site uses jwplayer as a video player, it has a very beautiful and easy API where a function there is called onComplete, where it will execute my javascript queries at the end of the video.

What I did is this:

onComplete:function() {
//Video has ended, now we redirect to the new video
window.location.href = "//www.mysite./video?id=33412";
}

So, using this, my site will successfully redirect to a new video, and so on. But, what if the user turns off the "Autoplay" feature as shown in the image. How can I disable the window.location.href?

Other stackoverflow answers said:

You can't change the window.location prototype, because this a "native property" of window and it is not configurable.

What would be the solution in my case? A solution using Jquery is okay for me too.

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Mar 20, 2015 at 17:18 Online UserOnline User 18k3 gold badges43 silver badges64 bronze badges 3
  • 4 Why don't you set a flag for autoplay. in onplete check the flag before window.location.href – mohamedrias Commented Mar 20, 2015 at 17:21
  • What do you mean by disable the window.location.href? – Evan Davis Commented Mar 20, 2015 at 17:21
  • 1 @mohamedrias solution is all you need, it's only needed a single flag... – silentw Commented Mar 20, 2015 at 17:21
Add a ment  | 

2 Answers 2

Reset to default 3

For autoplay have a flag. Set/unset the flag.

In your onComplete, check the flag

onComplete:function() {
//Video has ended, now we redirect to the new video

// check the flag and execute
if(autoPlay) window.location.href = "//www.mysite./video?id=33412";

}

You will need to store some state somewhere, such as this:

var autoplayEnabled = true;

$('#autoplay-toggle').on('click', function() {
    autoplayEnabled = ! autoplayEnabled;
});

// . . .
{
    onComplete: function() {
        // Video has ended, now we redirect to the new video
        if (autoplayEnabled) {
            window.location.href = "//www.mysite./video?id=33412";
        }
    }
}

When they click on the autoplay trigger it'll toggle the global variable which you then check against in the onComplete callback.

发布评论

评论列表(0)

  1. 暂无评论