javascript problems

Discussion in 'Web Design and Development' started by Cabbit, Apr 29, 2008.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    hi there i have two javascripts that just do not play well together. When it is just one of them they work perfect but if i need to use both of them it just dose not work at all.

    whosonline.js
    Code:
    GetXmlHttpObject = function()
    {
    var XMLHttp = null;
    // active x internetexplorer ---------------------------------------------------------
    try
    {
    	XMLHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ie7
    }
    catch (e)
    {
    try
    {
    	XMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //ie 6
    }
    catch(e)
    {
    try
    {
    	XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie older versions
    }
    catch(e)
    {
    }
    }
    }
    // active x internetexplorer ---------------------------------------------------------
    
    if (XMLHttp == null)
    {
    XMLHttp = new XMLHttpRequest();
    }
    return XMLHttp;
    }
    
    // Make the XMLHttpRequest object
    var http = GetXmlHttpObject();
    
    function sendRequest(page) {
    
    // Open PHP script for requests
    http.open('get', page);
    http.onreadystatechange = handleResponse;
    http.send(null);
    
    }
    
    function handleResponse() {
    
    if(http.readyState == 4 && http.status == 200){
    
    // Text returned FROM the PHP script
    var response = http.responseText;
    
    if(response) {
    // UPDATE ajaxTest content
    document.getElementById("whoonline").innerHTML = response;
    }
    
    }
    
    }
    
    function repeatloop()
    {
    sendRequest('../page_blocks/whos_online.php'); 
    setTimeout("repeatloop()", 50000);
    }
    
    window.onload=function() {
    repeatloop();
    }
    
    and fckeditor.js

    Code:
    /*
     * FCKeditor - The text editor for Internet - http://www.fckeditor.net
     * Copyright (C) 2003-2008 Frederico Caldeira Knabben
     *
     * == BEGIN LICENSE ==
     *
     * Licensed under the terms of any of the following licenses at your
     * choice:
     *
     *  - GNU General Public License Version 2 or later (the "GPL")
     *    http://www.gnu.org/licenses/gpl.html
     *
     *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
     *    http://www.gnu.org/licenses/lgpl.html
     *
     *  - Mozilla Public License Version 1.1 or later (the "MPL")
     *    http://www.mozilla.org/MPL/MPL-1.1.html
     *
     * == END LICENSE ==
     *
     * This is the integration file for JavaScript.
     *
     * It defines the FCKeditor class that can be used to create editor
     * instances in a HTML page in the client side. For server side
     * operations, use the specific integration system.
     */
    
    // FCKeditor Class
    var FCKeditor = function( instanceName, width, height, toolbarSet, value )
    {
    	// Properties
    	this.InstanceName	= instanceName ;
    	this.Width			= width			|| '100%' ;
    	this.Height			= height		|| '200' ;
    	this.ToolbarSet		= toolbarSet	|| 'Default' ;
    	this.Value			= value			|| '' ;
    	this.BasePath		= FCKeditor.BasePath ;
    	this.CheckBrowser	= true ;
    	this.DisplayErrors	= true ;
    
    	this.Config			= new Object() ;
    
    	// Events
    	this.OnError		= null ;	// function( source, errorNumber, errorDescription )
    }
    
    /**
     * This is the default BasePath used by all editor instances.
     */
    FCKeditor.BasePath = '/fckeditor/' ;
    
    /**
     * The minimum height used when replacing textareas.
     */
    FCKeditor.MinHeight = 200 ;
    
    /**
     * The minimum width used when replacing textareas.
     */
    FCKeditor.MinWidth = 750 ;
    
    FCKeditor.prototype.Version			= '2.6' ;
    FCKeditor.prototype.VersionBuild	= '18638' ;
    
    FCKeditor.prototype.Create = function()
    {
    	document.write( this.CreateHtml() ) ;
    }
    
    FCKeditor.prototype.CreateHtml = function()
    {
    	// Check for errors
    	if ( !this.InstanceName || this.InstanceName.length == 0 )
    	{
    		this._ThrowError( 701, 'You must specify an instance name.' ) ;
    		return '' ;
    	}
    
    	var sHtml = '' ;
    
    	if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
    	{
    		sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
    		sHtml += this._GetConfigHtml() ;
    		sHtml += this._GetIFrameHtml() ;
    	}
    	else
    	{
    		var sWidth  = this.Width.toString().indexOf('%')  > 0 ? this.Width  : this.Width  + 'px' ;
    		var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
    		sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ;
    	}
    
    	return sHtml ;
    }
    
    FCKeditor.prototype.ReplaceTextarea = function()
    {
    	if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
    	{
    		// We must check the elements firstly using the Id and then the name.
    		var oTextarea = document.getElementById( this.InstanceName ) ;
    		var colElementsByName = document.getElementsByName( this.InstanceName ) ;
    		var i = 0;
    		while ( oTextarea || i == 0 )
    		{
    			if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
    				break ;
    			oTextarea = colElementsByName[i++] ;
    		}
    
    		if ( !oTextarea )
    		{
    			alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
    			return ;
    		}
    
    		oTextarea.style.display = 'none' ;
    		this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
    		this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
    	}
    }
    
    FCKeditor.prototype._InsertHtmlBefore = function( html, element )
    {
    	if ( element.insertAdjacentHTML )	// IE
    		element.insertAdjacentHTML( 'beforeBegin', html ) ;
    	else								// Gecko
    	{
    		var oRange = document.createRange() ;
    		oRange.setStartBefore( element ) ;
    		var oFragment = oRange.createContextualFragment( html );
    		element.parentNode.insertBefore( oFragment, element ) ;
    	}
    }
    
    FCKeditor.prototype._GetConfigHtml = function()
    {
    	var sConfig = '' ;
    	for ( var o in this.Config )
    	{
    		if ( sConfig.length > 0 ) sConfig += '&' ;
    		sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
    	}
    
    	return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
    }
    
    FCKeditor.prototype._GetIFrameHtml = function()
    {
    	var sFile = 'fckeditor.html' ;
    
    	try
    	{
    		if ( (/fcksource=true/i).test( window.top.location.search ) )
    			sFile = 'fckeditor.original.html' ;
    	}
    	catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }
    
    	var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
    	if (this.ToolbarSet) sLink += '&Toolbar=' + this.ToolbarSet ;
    
    	return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
    }
    
    FCKeditor.prototype._IsCompatibleBrowser = function()
    {
    	return FCKeditor_IsCompatibleBrowser() ;
    }
    
    FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
    {
    	this.ErrorNumber		= errorNumber ;
    	this.ErrorDescription	= errorDescription ;
    
    	if ( this.DisplayErrors )
    	{
    		document.write( '<div style="COLOR: #ff0000">' ) ;
    		document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
    		document.write( '</div>' ) ;
    	}
    
    	if ( typeof( this.OnError ) == 'function' )
    		this.OnError( this, errorNumber, errorDescription ) ;
    }
    
    FCKeditor.prototype._HTMLEncode = function( text )
    {
    	if ( typeof( text ) != "string" )
    		text = text.toString() ;
    
    	text = text.replace(
    		/&/g, "&").replace(
    		/"/g, """).replace(
    		/</g, "<").replace(
    		/>/g, ">") ;
    
    	return text ;
    }
    
    ;(function()
    {
    	var textareaToEditor = function( textarea )
    	{
    		var editor = new FCKeditor( textarea.name ) ;
    
    		editor.Width = Math.max( textarea.offsetWidth, FCKeditor.MinWidth ) ;
    		editor.Height = Math.max( textarea.offsetHeight, FCKeditor.MinHeight ) ;
    
    		return editor ;
    	}
    
    	/**
    	 * Replace all <textarea> elements available in the document with FCKeditor
    	 * instances.
    	 *
    	 *	// Replace all <textarea> elements in the page.
    	 *	FCKeditor.ReplaceAllTextareas() ;
    	 *
    	 *	// Replace all <textarea class="myClassName"> elements in the page.
    	 *	FCKeditor.ReplaceAllTextareas( 'myClassName' ) ;
    	 *
    	 *	// Selectively replace <textarea> elements, based on custom assertions.
    	 *	FCKeditor.ReplaceAllTextareas( function( textarea, editor )
    	 *		{
    	 *			// Custom code to evaluate the replace, returning false if it
    	 *			// must not be done.
    	 *			// It also passes the "editor" parameter, so the developer can
    	 *			// customize the instance.
    	 *		} ) ;
    	 */
    	FCKeditor.ReplaceAllTextareas = function()
    	{
    		var textareas = document.getElementsByTagName( 'textarea' ) ;
    
    		for ( var i = 0 ; i < textareas.length ; i++ )
    		{
    			var editor = null ;
    			var textarea = textareas[i] ;
    			var name = textarea.name ;
    
    			// The "name" attribute must exist.
    			if ( !name || name.length == 0 )
    				continue ;
    
    			if ( typeof arguments[0] == 'string' )
    			{
    				// The textarea class name could be passed as the function
    				// parameter.
    
    				var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' ) ;
    
    				if ( !classRegex.test( textarea.className ) )
    					continue ;
    			}
    			else if ( typeof arguments[0] == 'function' )
    			{
    				// An assertion function could be passed as the function parameter.
    				// It must explicitly return "false" to ignore a specific <textarea>.
    				editor = textareaToEditor( textarea ) ;
    				if ( arguments[0]( textarea, editor ) === false )
    					continue ;
    			}
    
    			if ( !editor )
    				editor = textareaToEditor( textarea ) ;
    
    			editor.ReplaceTextarea() ;
    		}
    	}
    })() ;
    
    function FCKeditor_IsCompatibleBrowser()
    {
    	var sAgent = navigator.userAgent.toLowerCase() ;
    
    	// Internet Explorer 5.5+
    	if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
    	{
    		var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
    		return ( sBrowserVersion >= 5.5 ) ;
    	}
    
    	// Gecko (Opera 9 tries to behave like Gecko at this point).
    	if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
    		return true ;
    
    	// Opera 9.50+
    	if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.5 )
    		return true ;
    
    	// Adobe AIR
    	// Checked before Safari because AIR have the WebKit rich text editor
    	// features from Safari 3.0.4, but the version reported is 420.
    	if ( sAgent.indexOf( ' adobeair/' ) != -1 )
    		return ( sAgent.match( / adobeair\/(\d+)/ )[1] >= 1 ) ;	// Build must be at least v1
    
    	// Safari 3+
    	if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
    		return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ;	// Build must be at least 522 (v3)
    
    	return false ;
    }
    
    on my page they are called with other stuff.

    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    		<title>My Inbox</title>
    			<style type="text/css" media="all">@import "../stylesheet.css";</style>
    	<!--Scripts-->
    	<script type="text/javascript">
    	function setStyle(x)
    	{
    		document.getElementById(x).style.color="#000000";
    	}
    	function fade_text(x)
    	{
    		document.getElementById(x).style.color="#999999";
    	}
    	</script>
    		<script src="../jscript/whosonline.js" language="javascript" type="text/javascript"></script>
    	<!-- Script for text editor -->
    			<script src="../editor/fckeditor.js" language="javascript" type="text/javascript"></script>
    			<script type="text/javascript">
    				window.onload = function()
    					{
      						if (document.getElementById('editor'))
    						{
        					var oFCKeditor = new FCKeditor( 'editor' ) ;
       						oFCKeditor.BasePath = "../editor/" ;
     						oFCKeditor.ReplaceTextarea() ;
    						}
    						else {
    						}
    					}
    			</script>
    			<!-- End script for text editor -->
    	<!--End Sctipts-->
    </head>
    	<body>
    <!--*********************************************************************************-->
    <!--La La La ^_^ i made this. Me me me. I made this.-->
    <!--*********************************************************************************-->
    <!--Start page layout-->
    <div id="page">
    	<?php
    		///Read the header file, this has the navagtion bar and logo///
    		include("../page_blocks/header.php");
    	?>
    	<!--Main relates to the main content div for the entire page-->
    	<div class="main">
    			<!--Logged in - Not Logged in-->
    				<?php if($session->logged_in){ ?>
    		<!--The side bar is for elements like the clock and user logins-->
    		<div class="side_bar">
    												<?php
    				///Shows the clock///
    				///readfile("page_blocks/clock.php");
    				///User Control Panel///
    				include("../page_blocks/usercp.php");
    				///contrib///
    				include("../page_blocks/contrib.php");
    				///chating time///
    				include("../page_blocks/chat_time.php");
    				///whos online///
    			?>
    				<div id="whoonline"><noscript><?php include("../page_blocks/noscript_whos_online.php");?></noscript></div>
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    The short answer is you're over writing the onload event. It's used by the first script at the very end, then you assign a function to onload in your HTML. There's a solution though. Add the following function somewhere before you need it.

    JavaScript:
    Code:
    /*** Ensure onload isn't overwritten ***/
    function appendOnLoad(fx)
    { 
      var old = window.onload;
      if (typeof old != 'function') { window.onload = fx; }
      else { window.onload = function() { old(); fx(); } }
    }
    Then from your HTML you can do,
    Code:
    appendOnLoad( function()
    {
      if (document.getElementById('editor'))
      {
        var oFCKeditor = new FCKeditor( 'editor' ) ;
        oFCKeditor.BasePath = "../editor/" ;
        oFCKeditor.ReplaceTextarea() ;
      }
    });
    You'll probably want to do the same thing with the onload line from the first script because I'm not completely sure which will get called first.
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    FFIW are you using Firebug or the Web Developer toolbar.

    They have been very useful to me to help find Javascript bugs.
     
  4. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    thanks for the coding it solved the problem fine though i have yet to do the usual ie and firefox tests it works fine in Safari.
     

Share This Page