...

View Full Version : Noob in need of some quick javascript help PLEASE



leavetown
11-23-2011, 02:14 AM
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

Old Pedant
11-23-2011, 05:37 AM
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.



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";
}
}

leavetown
11-23-2011, 03:06 PM
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

leavetown
11-23-2011, 11:41 PM
Thank You so much Old Pedant for helping me with this problem and being patient with a clueless noob like myself.

chris

Old Pedant
11-24-2011, 12:23 AM
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 ***


<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)


<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)


<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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum