www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-28-2009, 09:51 PM
    oaishm oaishm is offline
    Registered User
     
    Join Date: Oct 2009
    Posts: 13
    Javascript doesn't work without firebug

    Does firebug do something special to force scripts to run? I found this script that unfortunately uses the document.write method. It doesn't run unless I have firebug console enabled. Then it runs fine.
    Code:
    <script type='text/javascript'>
    
    var spoilernum = 1;
    var fbuttons = new Array(); //This is for the buttons we will offer
    var fimagepath;
    
    function seturl(url){
    	fimagepath=url;
    	}
    
    function fbutton(innercontent, image, alt){
    	this.innercontent = innercontent; //this is the tag type
    	this.image = image; //button image
    	this.alt = alt; // the alt name for the image of the button
    	}
    	
    fbuttons.push(
    	new fbutton(
    		'url'
    		,'link.png'
    		,'Forum url'
    	)
    );
    
    fbuttons.push(
    	new fbutton(
    		'b'
    		,'bold.png'
    		,'Forum bold text button'
    	)
    );
    
    fbuttons.push(
    	new fbutton(
    		'i'
    		,'italics.png'
    		,'Forum italics text button'
    	)
    );
    
    fbuttons.push(
    	new fbutton(
    		'image'
    		,'image.png'
    		,'Forum image button'
    	)
    );
    
    
    function makebuttons() {
    	
    	var i = 0;
    	document.write('<div id="toolbar">');
    	for (i = 0; i < 4; i++) {
    		//console.log('loop number'+i);
    		//console.log(fimagepath+fbuttons[i].image);
    		console.log(i);
    		console.log('<a onclick="askBox(\''+fbuttons[i].innercontent+'\');" href="javascript: markup()"><img class="fbuttons" border="0" src="'+fimagepath+fbuttons[i].image+'" alt="'+fbuttons[i].alt+'"/></a>');
    		document.write('<a onclick="askBox(\''+fbuttons[i].innercontent+'\');" href="javascript: markup()"><img class="fbuttons" border="0" src="'+fimagepath+'templates/sbsru/images/'+fbuttons[i].image+'" alt="'+fbuttons[i].alt+'"/></a>');
    	}
    	document.write('</div>');
    }
    
    function countSpoiler() {
    	var spoilernum = spoilernum + 1;
    }
    
    function storeCaret(position) {
    	if (position.createTextRange){
    		position.caretPos = document.selection.createRange().duplicate();
    	}
    }
    
    function insertColor(Color) {
    	surroundText("[color:" + Color + "]" , "[/color]", document.post.body);
    	document.post.body.focus();
    	return;
    }   
    
    function askBox(what) {
    	if (what == "url") {
    		var ModText = prompt("Enter URL:", "http://");
    		if (ModText != null && ModText != '' && ModText != 'http://'){
    			var LinkText = prompt("Text For Link:","Link");
    			if (LinkText != null && LinkText != ''){ 
    				insertCode(document.post.body, ' ' + LinkText + ' ');
    			}
    		}
    		return;
    	} 
    	else if (what == "spoiler") {
    		var SpoilerQ = prompt("Spoiler Question " + spoilernum + ":","");
    		if (SpoilerQ != null && SpoilerQ != '') {
    			var SpoilerA = prompt("Spoiler Answer " + spoilernum + ":","");
    			if (SpoilerA != null && SpoilerA != '') {
    				insertCode(document.post.body, ' [spoilerq:' + spoilernum + ']' + SpoilerQ + '[/spoilerq] \n'
    				+ ' [spoilera:' + spoilernum + ']' + SpoilerA + '[/spoilera] ');
    				countSpoiler();
    			}
    		}	
    	}
    	else if (what == "image") {
    		var ModText = prompt("Enter URL to Image:", "http://");
    		if (ModText == null){
    			return;
    		}
    		insertCode(document.post.body, ' [image]' + ModText + '[/image] ');
    	}
    	else if (what == "color") {
    		var ModText = prompt("Enter Color Code:", "#");
    		if (ModText == null) {
    			return;
    		}
    		insertColor(ModText);
    	}
    	else if (what == "list") {
    		var ModText = "init";
    		var listoutput = "
    • \n"; while ( (ModText != null) && (ModText != "") ) { ModText = prompt("Enter List Item (leave blank to end list).",""); if ( (ModText != null) && (ModText != "") ) { listoutput = listoutput + "[li]" + ModText + "[/li]"; } } listoutput = listoutput + "
    \n "; insertCode(document.post.body, listoutput); } else { surroundText('['+ what + ']' , '[/'+ what + ']', document.post.body); } document.post.body.focus(); return; } function markup() { return; } function surroundText(text1, text2, textarea) { //M$ IE browsers if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; // remove white space at the start and end of selection selection = caretPos.text; var spacebefore = 0; var spaceafter = 0; while (selection.substring(0,1) == ' ') { selection = selection.substring(1); spacebefore = spacebefore + 1; } while (selection.substring(selection.length-1,selection.length) == ' '){ selection = selection.substring(0,selection.length-1); spaceafter = spaceafter + 1; } if(spacebefore > 0 ) { movedStart = caretPos.moveStart("character", +spacebefore); } if(spaceafter > 0 ) { movedEnd = caretPos.moveEnd ("character", -spaceafter); } textlength = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (textlength==0){ n = text2.length; caretPos.moveStart("character", -n); caretPos.moveEnd ("character", -n); } if (textlength > 0){ n = text1.length + text2.length + textlength; caretPos.moveStart("character", -n); } caretPos.select(); } //Mozilla browsers else if (typeof(textarea.selectionStart) != "undefined") { var start = textarea.selectionStart; var finish = textarea.selectionEnd; var begin = textarea.value.substr(0, start); var selection = textarea.value.substr(start, finish - start); var end = textarea.value.substr(finish); var newCursorPos = start; var scrollPos = textarea.scrollTop; // remove white space at the beginning and end of selection var spacebefore = 0; var spaceafter = 0; while (selection.substring(0,1) == ' ') { selection = selection.substring(1); spacebefore = spacebefore + 1; } while (selection.substring(selection.length-1,selection.length) == ' '){ selection = selection.substring(0,selection.length-1); spaceafter = spaceafter + 1; } if(spaceafter > 0 || spacebefore > 0 ) { start = start + spacebefore; finish = finish - spaceafter ; var begin = textarea.value.substr(0, start); var selection = textarea.value.substr(start, finish - start); var end = textarea.value.substr(finish); var newCursorPos = start; var scrollPos = textarea.scrollTop; } textarea.value = begin + text1 + selection + text2 + end; if (textarea.setSelectionRange){ if (selection.length == 0){ textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length); }else{ textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length); textarea.focus(); } textarea.scrollTop = scrollPos; } }else{ // other browsers textarea.value += text1 + text2; textarea.focus(textarea.value.length - 1); } } function insertCode(TextPosition, text) { if (TextPosition.createTextRange && TextPosition.caretPos) { //M$ IE browsers var caretPos = TextPosition.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; document.post.body.focus(); return; } //Mozilla browsers else if (typeof(TextPosition.selectionStart) != "undefined") { var begin = TextPosition.value.substr(0, TextPosition.selectionStart); var end = TextPosition.value.substr(TextPosition.selectionEnd); var scrollPos = TextPosition.scrollTop; TextPosition.value = begin + text + end; if (TextPosition.setSelectionRange) { TextPosition.focus(); TextPosition.setSelectionRange(begin.length + text.length, begin.length + text.length); } TextPosition.scrollTop = scrollPos; } // other browsers else{ TextPosition.value += text; TextPosition.focus(TextPosition.value.length - 1); } return true; } </script> <center> <form name="post" method="post" action="<?=$this->full_url?>"> <table border="0"> <tr> <td valign="top">Message:</td> <td> <script type="text/javascript">seturl('<?=$this->current_url?>');</script> <script type="text/javascript">makebuttons();</script> <textarea name="content" id="body" class="textbox" onchange="storeCaret(this);" onkeyup="storeCaret(this);" onclick="storeCaret(this);" onselect="storeCaret(this);" rows="10" cols="40"><?=$this->content?></textarea> <p><input type="submit" value="<?=$this->submit?>" /></p> </td> </tr> </table> </form> </center>

    Last edited by Kor; 11-30-2009 at 04:21 AM. Reason: wrap the code [code][/code]
    Reply With Quote
      #2  
    Old 11-28-2009, 11:54 PM
    opifex's Avatar
    opifex opifex is offline
    ignorance is curable...
     
    Join Date: Sep 2008
    Location: Mexico
    Posts: 784
    Do you have a live test version (link) or the actual html ? The php short tags contain variables that are missing in what you are showing us... that could be part of the problem that we can't see.

    Putting line breaks in the middle of code lines is a really bad idea and can cause problems, also you need to take care in what you choose to use for non-ascii symbols - I prefer unicode.

    var listoutput et al should be something like this...
    HTML Code:
    var listoutput = "\u00B7 \n";
    while ( (ModText != null) && (ModText != "") ) {
          ModText = prompt("Enter List Item (leave blank to end list).","");
          if ( (ModText != null) && (ModText != "") ) {
          listoutput = listoutput + "[li]" + ModText + "[/li]";
          }
          }
    listoutput = listoutput + "\n ";
    Have you turned on the error console in your browser? It might give you a clue.
    __________________
    ...but stupidity is terminal.

    Last edited by opifex; 11-29-2009 at 12:46 AM.
    Reply With Quote
      #3  
    Old 11-29-2009, 07:24 AM
    Specht08 Specht08 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 44
    console.log()

    you can't use console.log(); without firebug, comment those lines and try it again.
    Reply With Quote
      #4  
    Old 11-29-2009, 07:50 AM
    opifex's Avatar
    opifex opifex is offline
    ignorance is curable...
     
    Join Date: Sep 2008
    Location: Mexico
    Posts: 784
    @Specht08
    Nice catch! Would seem that OP copied the source code with Firebug open!

    Take out "console.log", do the corrections and it works as much as it can with out the unknown variables.
    __________________
    ...but stupidity is terminal.
    Reply With Quote
      #5  
    Old 11-29-2009, 02:13 PM
    oaishm oaishm is offline
    Registered User
     
    Join Date: Oct 2009
    Posts: 13
    I can't believe it

    I have to tell you that's amazing. I can understand php failing when you put in extraneous functions, but I guess I just thought Javascript would ignore unknown variables the way css would. I don't know why I thought that since it's retarded, but I did. Thanks so much, that's exactly what was wrong. This isn't as bad as when I misspelled javascript and debugged for hours, but its close.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    firebug


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:37 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.