当有人尝试关闭浏览器窗口/选项卡时进行管理

发布于 2024-11-19 11:14:07 字数 3723 浏览 3 评论 0原文

在我的 onbeforeunload 事件中,它询问用户是否要离开页面还是留在页面上。当他们单击“留在页面上”时,我希望它将他们重定向到同一窗口中的另一个网页。听起来很奇怪,但这就是我被指派做的事情。基本上,主页播放视频 - 我认为广告是为了购买东西,当它们关闭但随后决定留在页面上时,我们希望视频消失/停止播放并出现一些不同的信息(不同的“网页”) ”)。有没有办法做到这一点,而不仅仅是显示/隐藏 div?我可以重写该函数吗?我目前正在尝试这样做(如下所示),但现在对话框根本不显示。它之前就可以工作关于我做错了什么,或者如何完成这项任务有什么想法吗?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>The Shocking Truth - Cabot Market Letter</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
            $("#vid").show();
            $("#div2").hide();
        });
        var test = 1;
        function manipulatetest()
        {
            test = 2;
        }
    window.onbeforeunload = onbeforeunload_Handler;
    function onbeforeunload_Handler()
    {       if (test == 1){

                $("#vid").hide();
                $("#div2").show();
            var confirm = confirm("Would you like to stay on this page?");
            if (confirm == true) {
                window.location = "http://www.google.com";
                }

        }
}

</script>
</head>

<style type="text/css"> 

body {
    background-color: #e0e6e5;
}

#vid {  
    margin: 20px auto;
    width: 920px;
}

</style>


<body>

<div id="vid">
    <video width="920" height="540" autoplay preload controls>
      <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
      <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
      <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
      <object width="920" height="540" type="application/x-shockwave-flash"
        data="flowplayer-3.2.1.swf"> 
        <param name="movie" value="flowplayer-3.2.7.swf" /> 
        <param name="allowfullscreen" value="true" /> 
        <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
        <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
      </object>
    </video>
</div>

<div id="div2">
    <video width="920" height="540" autoplay preload controls>
      <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
      <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
      <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
      <object width="920" height="540" type="application/x-shockwave-flash"
        data="flowplayer-3.2.1.swf"> 
        <param name="movie" value="flowplayer-3.2.7.swf" /> 
        <param name="allowfullscreen" value="true" /> 
        <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
        <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
      </object>
    </video>
</div>

<p style="text-align: center;"><a href="http://www.cabot.net/info/cml/cmlld03.aspx?source=ed01" onclick="manipulatetest();">Click Here To Order</a></p>

</body>

In my onbeforeunload event, it asked the user whether they want to leave the page or stay on it. When they click "stay on page", I want it to then redirect them to another webpage in the same window. It sounds weird, but that's what I've been assigned to do. Basically, the main page plays a video - I think advertising to buy something, and when they close but then decide to stay on the page, we want the video to go away/stop playing and some different information to appear (a different "webpage"). Is there a way to do this, without just showing/hiding divs? Can I override the function? I'm currently trying to do it this way(as seen below) but now the dialog box is not showing up at all. It was working before Any ideas as to what I'm doing wrong, or how to accomplish this task?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>The Shocking Truth - Cabot Market Letter</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
            $("#vid").show();
            $("#div2").hide();
        });
        var test = 1;
        function manipulatetest()
        {
            test = 2;
        }
    window.onbeforeunload = onbeforeunload_Handler;
    function onbeforeunload_Handler()
    {       if (test == 1){

                $("#vid").hide();
                $("#div2").show();
            var confirm = confirm("Would you like to stay on this page?");
            if (confirm == true) {
                window.location = "http://www.google.com";
                }

        }
}

</script>
</head>

<style type="text/css"> 

body {
    background-color: #e0e6e5;
}

#vid {  
    margin: 20px auto;
    width: 920px;
}

</style>


<body>

<div id="vid">
    <video width="920" height="540" autoplay preload controls>
      <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
      <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
      <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
      <object width="920" height="540" type="application/x-shockwave-flash"
        data="flowplayer-3.2.1.swf"> 
        <param name="movie" value="flowplayer-3.2.7.swf" /> 
        <param name="allowfullscreen" value="true" /> 
        <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
        <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
      </object>
    </video>
</div>

<div id="div2">
    <video width="920" height="540" autoplay preload controls>
      <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
      <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
      <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
      <object width="920" height="540" type="application/x-shockwave-flash"
        data="flowplayer-3.2.1.swf"> 
        <param name="movie" value="flowplayer-3.2.7.swf" /> 
        <param name="allowfullscreen" value="true" /> 
        <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
        <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
      </object>
    </video>
</div>

<p style="text-align: center;"><a href="http://www.cabot.net/info/cml/cmlld03.aspx?source=ed01" onclick="manipulatetest();">Click Here To Order</a></p>

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-11-26 11:14:08

一个肮脏的伎俩是 setTimeout,看看它是否仍然运行:http://jsfiddle .net/FJ4LR/1/

window.onbeforeunload = function() {
    setTimeout(function() {
        alert('You clicked stay.');
    }, 500);

    return 'Really?';
};

A dirty trick is a setTimeout, and see whether it still runs: http://jsfiddle.net/FJ4LR/1/.

window.onbeforeunload = function() {
    setTimeout(function() {
        alert('You clicked stay.');
    }, 500);

    return 'Really?';
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文