Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 22nd, 2007, 01:21 PM
Registered User
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Difficulty writing javascript to access dom

Hi There;

I am trying to add some onClick functionality to a group of menu items in a template-based site by creating a javascript function that locates the a tag in this group and adds a new attribute.

It's not working :( Could someone please be kind to look at what i've written and tell me what i'm doing wrong?

Header script:

<script type="text/javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {

function addAtt() {
  if ( !document.getElementById ||
        !document.createElement ||
        !document.getElementsByTagName ) {
  var primaryDiv = document.getElementById("navlist");
  var primarylinks = primaryDiv.getElementsByTagName("a");
  for ( var i = 0; i < primarylinks.length; i++ ) {


and in the body:
<div class="secondnav">
    <ul class="links" id="navlist">
        <li class="first menu-2-1-49">
            <a href="#" class="menu-2-1-49">style</a></li>

<div class="secondnav" id="d6" style="display:none">
    <ul class="links" id="subnavlist">
        <li class="first menu-2-1-49">
            <a href="/dave/node/3" class="menu-2-1-49">style</a></li>
Old February 23rd, 2007, 07:32 AM
Friend of Wrox
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts

Not sure but something jumped out at me straight away:

this line:

if (typeof window.onload != 'function') {

Should surely be:

if (typeof(window.onload) != 'function') {

Not even sure that will work TBH.

'sync' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.
Old February 23rd, 2007, 11:25 AM
richard.york's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

You actually don't have to place parenthesis around window.onload. typeof, as a language construct does not require these, since it is not a function.

Personally, I always put those in, but they're technically optional.


Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in writing anything within "" by XML DOM. Somesh XML 0 March 5th, 2007 07:45 AM
Problem in writing within "" by DOM. Somesh XSLT 1 March 5th, 2007 07:27 AM
Problem in writing anything within "" by XML DOM. Somesh C# 0 March 5th, 2007 07:08 AM
Javascript Confirm dialog difficulty ayrton Javascript 1 May 20th, 2005 03:34 AM
Problem writing xml file using DOM Bala XML 0 September 6th, 2003 02:15 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.