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
https://forums.phpfreaks.com/topic/145426-problem-calling-a-function-from-html/
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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.