vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Odd error from script in header (https://vborg.vbsupport.ru/showthread.php?t=252940)

SBlueman 10-31-2010 05:00 PM

Odd error from script in header
 
This script which is in my header include template is this:

Code:

<script type="text/javascript">

Array.prototype.contains = function (ele) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == ele) {
            return true;
        }
    }
    return false;
};

Array.prototype.remove = function (ele) {
    var arr = new Array();
    var count = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i] != ele) {
            arr[count] = this[i];
            count++;
        }
    }
    return arr;
};

window.onload = function () {
        var taglist = document.getElementById('tags');
        taglist.value = taglist.value.replace (/\s+/g,'');  // strip space
        var tags = taglist.value.split(',');
       
        var populartags = document.getElementById('popularTags').getElementsByTagName('span');
       
        for (var i = 0; i < populartags.length; i++) {
            if (tags.contains(populartags[i].innerHTML)) {
                populartags[i].className = 'selected';
            }
        }
}

function doTag(ele) {
    var thisTag = ele.innerHTML;
    var taglist = document.getElementById('tags');
    var tags = taglist.value.split(',');
 
    // If tag is already listed, remove it
    if (tags.contains(thisTag)) {
        tags = tags.remove(thisTag);
        ele.className = 'unselected';
       
    // Otherwise add it
    } else {
        tags.splice(tags.length-1, 0, thisTag);
        ele.className = 'selected';
    }
   
    taglist.value = tags.join(',');
    document.getElementById('tags').focus();
}
</script>

I have to have this code because it is key to critical feature on the site. Unfortunately it seems to be causing some issues to my message editor. When I go to select a font or font size, I am getting this odd link at the bottom of my font drop-down:

http://img185.imageshack.us/img185/3595/errorcl.jpg

I am also getting an error with the font size drop-down menu:
http://img177.imageshack.us/img177/4323/error2hq.jpg

Is there a way to isolate the script and have it called from somewhere else other than the template and would this fix the template error I am experiencing?

Also....and I don't know if this is related.....

I am getting this error when selecting threads:

http://img684.imageshack.us/img684/3673/error3zt.jpg

I have two checked.....and the counter says four. I uncheck them, and the number changes. It's acting all squirrelly.

Lynne 10-31-2010 05:29 PM

Where in the headinclude template did you add that code? I just added it to mine with no problem... the page is just fine for me.

SBlueman 10-31-2010 05:40 PM

I added it at the very end of my headinclude template.

--------------- Added [DATE]1288550490[/DATE] at [TIME]1288550490[/TIME] ---------------

BTW, it also seems like I can't seem to use the advanced WYSIWYG editor without issues either.

kh99 10-31-2010 05:50 PM

I think the problem is that the javascript code that builds the color and font menus (in clientscript/vbulletin_textedit.js) uses for...in loops to add the items, and that kind of loop gets all properties of an object, including anything added to the prototype. (As explained here: http://www.prototypejs.org/api/array).

So maybe the easiest thing to do is to change that JS code that you've added. It wouldn't be that hard to move the "contains" and "remove" code in to the code below where they're called (assuming of course that there isn't any more JS code on that page that relies on those functions).

SBlueman 10-31-2010 06:12 PM

The script is for this modification:

https://vborg.vbsupport.ru/showthread.php?t=159646

I use it to create pre-defined topic tags that members can click once on to select. The problem is that this code was setup by a former member of the site who we have lost touch with.

kh99 10-31-2010 06:25 PM

OK, I haven't tried it at all, but maybe try this code instead of what you originally posted:

Code:

<script type="text/javascript">

function contains(a, ele) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] == ele) {
            return true;
        }
    }
    return false;
};

function remove(a, ele) {
    var arr = new Array();
    var count = 0;
    for (var i = 0; i < a.length; i++) {
        if (a[i] != ele) {
            arr[count] = a[i];
            count++;
        }
    }
    return arr;
};

window.onload = function () {
        var taglist = document.getElementById('tags');
        taglist.value = taglist.value.replace (/\s+/g,'');  // strip space
        var tags = taglist.value.split(',');
       
        var populartags = document.getElementById('popularTags').getElementsByTagName('span');
       
        for (var i = 0; i < populartags.length; i++) {
            if (contains(tags, populartags[i].innerHTML)) {
                populartags[i].className = 'selected';
            }
        }
}

function doTag(ele) {
    var thisTag = ele.innerHTML;
    var taglist = document.getElementById('tags');
    var tags = taglist.value.split(',');
 
    // If tag is already listed, remove it
    if (contains(tags, thisTag)) {
        tags = remove(tags, thisTag);
        ele.className = 'unselected';
       
    // Otherwise add it
    } else {
        tags.splice(tags.length-1, 0, thisTag);
        ele.className = 'selected';
    }
   
    taglist.value = tags.join(',');
    document.getElementById('tags').focus();
}
</script>


SBlueman 10-31-2010 10:04 PM

That fixed all the issues I was having with this modification. Thank you so much!

What was the change that you made and why? I'd love to know to learn.

kh99 10-31-2010 10:25 PM

Well the "Array.prototype.contains = function (ele)..." from the original code adds a function to the javascript 'Array' class so that later you can do something like

PHP Code:

var = new Array();
...
if (
x.contains('foo'))


to check if the array contains the value 'foo'. But adding the functions to the class was causing the problem because of the use of for..in loops by the code that builds the editor menus (see the link I posted above). So I changed it to


PHP Code:

function contains(aele)...

var 
= new Array();
...
if (
contains(x'foo'))


which does the same thing by adding a parameter to a normal function instead of adding a function to the class.

Hopefully this makes some sense.

SBlueman 11-01-2010 01:30 AM

Looks like it's time for me to learn some things about java. Thank you for pointing me in the right direction.

Lynne 11-01-2010 02:27 AM

First thing to learn is that java and javascript are NOT the same language. (I just don't want you to get a book about java when what you really want is a book about javascript. :) )


All times are GMT. The time now is 01:12 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01016 seconds
  • Memory Usage 1,750KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete