Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Noob in need of some quick javascript help PLEASE

    I am working on a site for an online radio station and I want an external player to launch on site visit and to continue playing as the visitors surf my site.

    I feel I am 95% there as I have the player spawning on launch and checking for popup blockers.

    If a popup blocker is found the user is prompted to add my domain to their allowed lists so that future visits will launch the player right out of the box.

    My problem is this. When someone navigates back to the "home" page (where the script is located the external player window reloads and I do not want it to.

    I have found a couple of sites that say do this and that but I am a 100% novice at javascript and can not figure it out.

    here is the url of the site I am building http://www.radiotrips.com/czmradio - If you have popup blocker on you should get an alert. If not a small player should launch

    here is the code that I am using to spawn the external player and check for popup blockers

    **************

    <script language="javascript" type="text/javascript">

    function detectPopupBlocker() {
    var czmTest = window.open("http://www.radiotrips.com/czmradio/player/mini/no_ads.html","Cozumel Radio","width=478,height=260");
    if (!czmTest) {
    alert("We were unable to automatically open the Cozumel Radio Player due to your pop-up blocker... Please add CozumelRadio.net to your allowed sites for your best listening experience");
    } else {
    window.open("http://www.radiotrips.com/czmradio/player/mini/no_ads.html","Cozumel Radio","width=478,height=260");
    }
    }

    **************

    if anyone could add the code that would make the browser check to see if this window is already open and if so not reload it I would GREATLY appreciate it.

    I feel like it is 1 to 2 lines of code max according to what I have seen elsewhere but just cant figure it out.

    Thanks a Million in advance
    Chris

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    First of all, your current code is opening the popup *TWICE*.

    The first time when you assign the result to czmTest and then again in the else.

    WHY???

    Anyway, the simple way to do this is use a session cookie.

    If the user has cookies disabled, then the player will restart. But tough.

    If the user has JavaScript disabled, then nothing works. Also tough.

    For most people it should be fine.

    Code:
    function detectPopupBlocker( )
    {
        if ( document.cookie.indexOf("csmradioIsPlayingNow") >= 0 ) return; // already playing
        var czmTest = 
                window.open("http://www.radiotrips.com/czmradio/player/mini/no_ads.html",
                            "Cozumel Radio",
                            "width=478,height=260");
        if (czmTest==null) 
        {
            alert("We were unable to automatically open the Cozumel Radio Player due to your pop-up blocker...\n"
                  + "Please add CozumelRadio.net to your allowed sites for your best listening experience");
        } else {
            document.cookie = "csmradioIsPlayingNow=yes";
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    leavetown (11-23-2011)

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    would this go in place of my existing code? I used this and nothing happened at all in any browser... did not detect popup blocker where active and on sites with no popup blocker the player window did not even launch.

    I must be missing something.

    PS I also noticed in some of the code you have "csmradioIsPlayingNow" should that be "czm"?? even so I changed it in the code on my site with the same result.

    any further help is greatly appreciated.

    chris

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You so much Old Pedant for helping me with this problem and being patient with a clueless noob like myself.

    chris

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    For anyone else reading this, here is the solution I finally came up with:

    How it works: If you open a window WITH A NAME but do *NOT* give it a URL, then if the window already exists, *NOTHING HAPPENS* except that you *do* get a reference to the already open window!

    Okay, so far, so good. So now we just need to test if the window is playing the music. Oops...we can't test anything like that via JavaScript because the player is in a different domain! So...my trick: The window is a URL in *YOUR* domain but it simply includes an <iframe> that has the music player!

    So...

    *** junk1.html ***
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function detectPopupBlocker( )
    {
        var czm = null;
        czm = window.open("",
                          "CozumelRadio",
                          "width=478,height=260");
        // for debug: alert("czm window found?" + (czm !=null) );
        // for debug: if ( czm!=null) alert(czm.playing);
    
        if ( czm != null && czm.playing != null ) return; // already playing
    
        // okay, not already playing...did the window open?
        if (czm == null) 
        {
            alert("We were unable to automatically open the Cozumel Radio Player due to your pop-up blocker...\n"
                  + "Please add CozumelRadio.net to your allowed sites for your best listening experience");
            return;
        }
        // so the window opened, but nothing is playing...
        // so give it the right URL:
        czm.location.href = "playit.html";
    }
    window.onload = detectPopupBlocker;
    </script>
    </head>
    <html>
    <body>
    well, we go here.
    <hr>
    <a href="junk2.html">go someplace else</a>
    </body>
    </html>
    *** playit.html *** (the page that holds the player)
    Code:
    <html>
    <head>
    <title>Cozumel Radio</title>
    <script type="text/javascript">
    var playing = "yes!";  // the existence of this is all that matters
    </script>
    <style type="text/css">
    * { margin: 0px; padding: 0px; }
    </style>
    </head>
    <body>
    <iframe style="width: 100%; height: 100%;"
            scrolling="no" 
            src="http://www.radiotrips.com/czmradio/player/mini/no_ads.html"
    ></iframe>
    </body>
    </html>
    *** junk2.html *** (silly dummy page just to demo it)
    Code:
    <html><body>
    This is junk2.html
    <hr>
    <a href="junk1.html">go back</a>
    </body></html>
    It works! Maybe best part: Because the popup is coming from the SAME DOMAIN as your main page, some popup blockers on low or medium settings will allow it. (Even if they don't you are no worse off.)

    How's that for tricky?
    Last edited by Old Pedant; 11-24-2011 at 12:26 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •