Forum Moderators: open

Message Too Old, No Replies

BBcode

         

Borgscan

9:15 am on Sep 16, 2008 (gmt 0)

10+ Year Member



Hello,

I've created a BBcode system (PHP) but I'm having a problem with the JavaScript where the buttons that use a prompt box can only be pressed once and not once for each button.


<script language="javascript" type="text/javascript">
<!--
function bbcode_replace(bbcode_name)
{
switch (bbcode_name)
{
case 'link':
arr = ['Please enter the URL below.', 'Please enter a name or description for the URL'];
return arr;
break;

case 'heading':
arr = ['Please enter the heading below.'];
return arr;
break;

case 'subheading':
arr = ['Please enter the subheading below.'];
return arr;
break;

default:
return false;
}
}

function bbcode_search(bbcode_name)
{
switch (bbcode_name)
{
case 'link':
arr = ['var1', 'var2'];
return arr;
break;

case 'heading':
arr = ['var1'];
return arr;
break;

case 'subheading':
arr = ['var1'];
return arr;
break;

default:
return false;
}
}

function bbcode(form_name, element_name, type, bbcode, bbcode_name)
{
if (type == 1)
{
var input = document.forms[form_name].elements[element_name];
input.value = input.value+bbcode;
}
if (type == 2) // requires user input
{
bbcode_replace = bbcode_replace(bbcode_name);
bbcode_search = bbcode_search(bbcode_name);
if (bbcode_replace != false)
{
for (var i in bbcode_replace)
{
var replace = prompt(bbcode_replace[i], "");
if (replace != 'undefined' && replace != null)
{
if (replace == "")
{
replace = "";
}
for (var j in bbcode_search)
{
if (i == j)
{
var search = bbcode_search[j];
bbcode = bbcode.replace(search, replace);
}
}
}
}
if (replace != 'undefined' && replace != null)
{
var input = document.forms[form_name].elements[element_name];
input.value = input.value+bbcode;
}
}
else
{
alert("Sorry, there appears to be a problem with this function.");
}
}
}
//-->
</script>

...

<a href="javascript:bbcode('edit_content', 'content', '1', '[bold]TEXT[/bold]', '');" title="Bold">Bold</a>
<a href="javascript:bbcode('edit_content', 'content', '2', '[link=var1]var2[/link]', 'link');" title="URL">URL</a>

The problem isn't with the bold tag. I can click it and have it add the code to the textarea (name=content) as many times as I like, but the URL one doesn't. I'm using the searrch and replace arrays because those functions will by dynamically populated by PHP and MySQL so that an administrator can make custom BBcodes.

JavaScript is not my forte but I suspect it has something to do with the arrays.

Any help would be much appreciated!

[edited by: Borgscan at 9:37 am (utc) on Sep. 16, 2008]

Borgscan

9:40 am on Sep 16, 2008 (gmt 0)

10+ Year Member



Please disregard. A friend pointed out that two of my variables were the same name as the functions. If anyone wants to use this, here is the fix.


<script language="javascript" type="text/javascript">
<!--
function bbcode_replace_arrays(bbcode_name)
{
switch (bbcode_name)
{
case 'url':
arr = ['Please enter the URL below.', 'Please enter a name or description for the URL'];
return arr;
break;

case 'heading':
arr = ['Please enter the heading below.'];
return arr;
break;

case 'subheading':
arr = ['Please enter the subheading below.'];
return arr;
break;

default:
return false;
}
}

function bbcode_search_arrays(bbcode_name)
{
switch (bbcode_name)
{
case 'url':
arr = ['var1', 'var2'];
return arr;
break;

case 'heading':
arr = ['var1'];
return arr;
break;

case 'subheading':
arr = ['var1'];
return arr;
break;

default:
return false;
}
}

function bbcode(form_name, element_name, type, bbcode, bbcode_name)
{
if (type == 1)
{
var input = document.forms[form_name].elements[element_name];
input.value = input.value+bbcode;
}
if (type == 2)
{
bbcode_replace = bbcode_replace_arrays(bbcode_name);
bbcode_search = bbcode_search_arrays(bbcode_name);
if (bbcode_replace != false)
{
for (var i in bbcode_replace)
{
var replace = prompt(bbcode_replace[i], "");
if (replace != 'undefined' && replace != null)
{
if (replace == "")
{
replace = "";
}
for (var j in bbcode_search)
{
if (i == j)
{
var search = bbcode_search[j];
bbcode = bbcode.replace(search, replace);
}
}
}
}
if (replace != 'undefined' && replace != null)
{
var input = document.forms[form_name].elements[element_name];
input.value = input.value+bbcode;
}
}
else
{
alert("Sorry, there appears to be a problem with this function.");
}
}
}
//-->
</script>

...

<a href="javascript:bbcode('edit_content', 'content', '1', '[bold]TEXT[/bold]', '');" title="Bold">Bold</a>
<a href="javascript:bbcode('edit_content', 'content', '2', '[link=var1]var2[/link]', 'link');" title="URL">URL</a>