Jump to content


Photo

Javascript run once


  • Please log in to reply
2 replies to this topic

#1 Spyder

Spyder

    Jedi In Training

  • Members
  • PipPip
  • 438 posts
  • Gender:Male
  • Location:London, UK

Posted 03 March 2011 - 05:13 AM

I am running a flash detection script which I will include below...

It is called by...

<script type="text/javascript">
//<![CDATA[
var requiredFlashVersion = 8;
var hasFlashURL = 'http://www.*****.org.uk/';
var upgradeFlashURL  = document.location.href + "?flash=ug";
var noFlashURL  = 'http://www.*****.org.uk/text_version/**********.html';
var dontKnowFlashVersionURL = document.location.href + "?flash=dk";
//]]>
</script>
<script type="text/javascript" src="flash_detect.js">
//<![CDATA[
function getFlashVersion() { return null; };
//]]>
</script>
<script type="text/javascript" src="flash_redirect.js">
//<![CDATA[
location.href = dontKnowFlashVersionURL;
//]]>
</script>
as you can see it determines whether the user has flash and refreshes the page with the according url paramater or sends them on their way.

The problem I have is that because it refreshes the page the script runs again and again and just readds the url paramater every time... so it keeps refreshing until the url looks like this: index.html?flash=dk?flash=dk?flash=dk?flash=dk?flash=dk.

I want a way to make it realise it has done its job and not run after its first service. Everything I have tried such as using an incremental variable has lead to it loading index.html/undefined (I probably executed this wrong).

Here is the external js files for the redirection:

flash_detect.js
// Flash Version Detector  v1.2.1
// documentation: http://www.dithered.com/javascript/flash_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)
// with VBScript code from Alastair Hamilton (now somewhat modified)


function isDefined(property) {
  return (typeof property != 'undefined');
}

var flashVersion = 0;
var count = 0;
function getFlashVersion() {

	var latestFlashVersion = 8;
   var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }

   if (agent.indexOf("firefox") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = -2;
   }

	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			for (var i = latestFlashVersion; i >= 3; i--) {
            if (flashPlugin.description.indexOf(i + '.') != -1) {
               flashVersion = i;
               break;
            }
         }
		}
	}

	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
	   var doc = '<scr' + 'ipt language="VBScript"\> \n';
      doc += 'On Error Resume Next \n';
      doc += 'Dim obFlash \n';
      doc += 'For i = ' + latestFlashVersion + ' To 3 Step -1 \n';
      doc += '   Set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash." & i) \n';
      doc += '   If IsObject(obFlash) Then \n';
      doc += '      flashVersion = i \n';
      doc += '      Exit For \n';
      doc += '   End If \n';
      doc += 'Next \n';
      doc += '</scr' + 'ipt\> \n';
      document.write(doc);
   }
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
	}

	return flashVersion;
}

flashVersion_DONTKNOW = -1;


flash_redirect.js
// Flash Detection / Redirect  v1.1.1
// documentation: http://www.dithered.com/javascript/flash_redirect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)
// requires: flash_detect.js (http://www.dithered.com/javascript/flash_detect/index.html)


// use flash_detect.js to return the Flash version
var flashVersion = getFlashVersion();

// Redirect to appropriate page


if (flashVersion >= requiredFlashVersion) location.replace(hasFlashURL); 
else if (flashVersion > 0) location.replace(upgradeFlashURL);
else if (flashVersion == -2) location.replace(dontKnowFlashVersionURL); 
else if (flashVersion == 0) location.replace(noFlashURL); 
else if (flashVersion == flashVersion_DONTKNOW || flashVersion == null) location.replace(dontKnowFlashVersionURL);

Your help is appreciated but please go into detail as I have a minor understanding of Javascript.

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 March 2011 - 11:01 AM

Before running everything, just check to see if window.location.query has the flash parameter and that it contains an acceptable value.

#3 Spyder

Spyder

    Jedi In Training

  • Members
  • PipPip
  • 438 posts
  • Gender:Male
  • Location:London, UK

Posted 07 March 2011 - 05:45 AM

Thanks I was using an outdated script and I switched to swfobject and just put this before it works out flashversion:

if (document.location.href.indexOf('flash') > 0) {
	return false;
}

works a treat.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users