Jump to content

problem calling a function from html


woodsonoversoul

Recommended Posts

Hi all, it seems like some times this works and other times it doesn't and I can't figure out why. My problem is that this line of html:

<image id="expImg" src="16-arrow-right.png" onclick="expand('resultsDiv', 'artist', 'artist');"></image>

 

Does not call this function:

function expand(group, artist, expandTo){
  alert("expand called");
  if(group == 'resultsDiv'){
    var calling_element = document.getElementById('result_root');
    }
  else{
    var calling_element = document.getElementById(group);
    }
  var calling_element_img = calling_element.getElementsByTagName('img');
  var calling_img_src = calling_element_img[0].getAttribute('src');
  
  if(calling_element_img[0].getAttribute('src') == '16-arrow-right.png'){    
    calling_element_img[0].setAttribute('src', '16-arrow-down.png');
    process(group, artist, expandTo);
    }
  else{
    calling_element_img[0].setAttribute('src', '16-arrow-right.png');
    removeChildren(group);
    }
}

 

even though the script file is referenced in <head>:

<script type="text/javascript" src="xml.js"></script>

 

I can post the whole code, but does anyone see any obvious errors?

Thanks

Link to comment
Share on other sites

I don't understand why it wouldn't be. And the thing is is that it only throws this error sometimes (which is even more unnerving than having a constant error). For instance, I'll go in and change a few things in my javascript file and it will say expand() is not defined, but when I try later in the afternoon it will work fine. What gives? Have you heard about anything like this before?

Link to comment
Share on other sites

there are a lot of factors that cause that. small changes in your HTML can even cause the JavaScript file not to load. also, check to make sure you are not executing any javascript until after the DOM is done loading (load JS if fine, just don't execute any functions). also, check to make sure your browser isn't caching the JS file. an easy solution to this is loading your file with:

<script type="text/javascript" src="scripts/myfile.js?_=<?php echo time(); ?>"></script>

that way, during development, you know it's loading the latest copy. once in production, i would remove that extra bit of PHP

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.