NameSilo

[Javascript] Error in Google chrome

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Basically my bbcode insertion javascript works near on perfect in all browsers now apart from chrome. It seems to repeat whatever is before it when you enter text:

Code:
// add simple text
function addText(Text)
{
	var obj = document.form.message;
	obj.focus();
 
	if (document.selection && document.selection.createRange)  // Internet Explorer
	{
		sel = document.selection.createRange();
		if (sel.parentElement() == obj)  
		{
			sel.text = Text;
		}
	}

	else if(typeof(obj) != 'undefined')  // Firefox
	{
		var longueur = parseInt(obj.textLength);
		var selStart = obj.selectionStart;
		var selEnd = obj.selectionEnd;

		obj.value = obj.value.substring(0,selStart) + Text + obj.value.substring(selEnd,longueur);
	}
 
	else 
	{
		obj.value += Text;
	}

	obj.focus();
}

// wrap code in tags or just simply add them by themselves
function addTags(Tag, fTag)
{
	var obj = document.form.message;
	obj.focus();

	// Internet Explorer
	if (document.selection && document.selection.createRange) 
	{
		sel = document.selection.createRange();
		if (sel.parentElement() == obj)
		{
			sel.text = Tag + sel.text + fTag;
		}
	}

	// Firefox
	else if(typeof(obj) != 'undefined')
	{
		var longueur = parseInt(obj.textLength);
		var selStart = obj.selectionStart;
		var selEnd = obj.selectionEnd;

		obj.value = obj.value.substring(0,selStart) + Tag + obj.value.substring(selStart,selEnd) + fTag + obj.value.substring(selEnd,longueur);
	}
 
	else
	{	
		obj.value += Tag + fTag;
	}

	obj.focus();
}

You can test here:
Prxa.info Message Board >> Add New Topic

In chrome it will delete whatever was before it when entering the bbcode into the box and repeat stuff, test above to see what i mean :(
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
I am not really sure what the fix would be. I use jQuery which will work across all browsers. Doing it from scratch can be a pain since all of the browsers render javascript differently.
 
0
•••
JQuery is way too big for what i need it for, besides this works fine in every browser apart from chrome :(
 
0
•••
Chrome has an awesome debugging console. The line throwing out problems is:
Code:
var longueur = parseInt(obj.textLength);

Fortunately, there is an easy fix!
Just use:
Code:
var longueur = parseInt(obj.value.length);

That's tested in Chrome but not Firefox. You should be fine though, obj.value will always return the text and .length will return the length of a string.


Bruce
 
0
•••
Excellent, didn't know Chrome has that, works like a charm across all major browsers now, thanks! :D
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back