views:

478

answers:

3

Hi folks, I'm really very new to javascript, and trying to validate a page to xhtml transitional. I use onselectstart="return false"

So I understand that I am wanting to create a javascript function that will insert that as an id. I even found this http://www.webmasterworld.com/javascript/3054096.htm and he figured out how to do it.

He is putting the onload in the body and setting the ids. Can I do this with a class and not set specific ID numbers?

A: 

EDIT - This modified answer incorporates my previous answer with the answer provided by CMS.

This code works in IE 6/7/8:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title>test page</title>
    <script type="text/javascript" language="javascript">
        window.onload = function() {
            var elements = getElementsByClassName(document, 'noselect');
            for (var i = 0; i < elements.length; i++) {
                elements[i].attachEvent('onselectstart', rfalse);
            }
        }

        function rfalse() { return false; }

        function getElementsByClassName(node, classname) {
            if (node.getElementsByClassName) { // use native implementation if available
                return node.getElementsByClassName(classname);
            } else {
                return (function getElementsByClass(searchClass, node) {
                    if (node == null)
                        node = document;
                    var classElements = [],
                els = node.getElementsByTagName("*"),
                elsLen = els.length,
                pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"), i, j;

                    for (i = 0, j = 0; i < elsLen; i++) {
                        if (pattern.test(els[i].className)) {
                            classElements[j] = els[i];
                            j++;
                        }
                    }
                    return classElements;
                })(classname, node);
            }
        }
    </script>
</head>
<body>
    <h2 class='noselect'>
        this text cannot be selected
    </h2>
    <h2>
        this text can be selected
    </h2>
    <h2 class='noselect'>
        this text cannot be selected
    </h2>
</body>
</html>
Gabriel McAdams
This looks short and sweet-sorry for my noob question...will I just add this to my external functions include file, and then any time I use the 'myclass' in a <element class="myclass">, this will link to it?
Joel
depending on the browser, etc.. yes. You should look at the function in the answer provided by CMS also (some browser versions don't have the getElementsByClassName function).
Gabriel McAdams
You should either use `elements[i].onselectstart = ...` or add support for non IE browsers as well.
Justin Johnson
in this specific instance, I only need IE support as the onselectstart="return false" is purely for IE...that said, i still have not been able to get this to work with the above code.
Joel
What kind of elements are you dealing with?
Gabriel McAdams
See my modified answer (I used my previous answer along with the function provided by CMS). I tested it in IE 6, 7, and 8 (it works in all).
Gabriel McAdams
+2  A: 

You can use the document.getElementsByClassName method, but it isn't standard yet (it will be part of HTML5), you can be completely sure that it will not work on any IE version, some modern browsers provide a native implementation, but if it isn't available, a loop checking for the specific class you look for can be done.

I personally use the following function, inspired by the Dustin Diaz implementation:

function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

Then you can use it like this:

window.onload = function () {
  var returnFalse = function () { return false; },
      els = getElementsByClassName(document, 'yourClassName'),
      n = els.length;

  while (n--) {
    els[n].onselectstart = returnFalse; 
  }
};
CMS
ok Great...again-sorry for the newbe question. So I will put the function in the external functions page, but the second variables there, does that go in the head section of the page, or also in that external file?
Joel
@Joel: I would consider making an external file of the second snippet *only if it will be reused*, otherwise you can put it in your page.
CMS
OK great-sorry to turn this into a forum :D...The "yourClassname" makes good sense...On the first part, I see (node,classname) adn (classname) should I be replacing that with my class name? If so, what is "node"?
Joel
@Joel, no, you don't have to modify the function I posted `classname ` is just a function argument, you should only change `'yourClassName'` to your real class name on the second snippet...
CMS
damn...I'm doing something wrong...OK. So I have the above code in my external.js with 'yourClassName' changed to 'unselectable' Then in my page, for example I have this: echo "<h2 class='logintop unselectable' >Welcome</h2> See what I'm missing?
Joel
A: 

None of the above solutions worked for me-though I'm sure that was due to user error. We have Jquery already running, and turns out they are already set up for this. We used this code in the head section, and it works great!

<script language="javascript" type="text/javascript">
$(document).ready(function() {
//alert($(".unselectable").length)
var returnFalse = function () { return false; },
els = $(".yourClassNameHere"),
n = els.length;
while (n--) {
els[n].onselectstart = returnFalse;
}
});
</script>
Joel
Why didn't you tell us that you were using JQuery?
Gabriel McAdams
Because as I said in the first post-I'm new to this, and i don't know what is and is not important to tell you. Have a good day.
Joel