I’m using getElementsByTagName() to retrieve some elements and do something with them. It works fine in FF but not in Chrome and Safari. Please can someone tell me what to do, I’m really stuck? Thanks in advance.
In Chrome it says: “Uncaught TypeError: Cannot call method ‘getElementsByTagName’ of null”
Here’s the code:
function popUpSAPWindow(){
// Find all links in the page and put them into an array.
[COLOR="Red"]//Below is the line that gives me trouble[/COLOR]
var linksInOrderLinesTable = document.getElementById("orderLinesTable").getElementsByTagName("a");
var linksLen = linksInOrderLinesTable.length;
// If the link text is 'SAP' then modify the attributes
for(var i = 0; i < linksLen; i++){
if(linksInOrderLinesTable[i].innerHTML == "SAP"){
// Store the 'href' value of each SAP link.
var sapHref = linksInOrderLinesTable[i].href;
// Modify the attributes of each SAP link.
linksInOrderLinesTable[i].setAttribute("href", "javascript:return false;");
linksInOrderLinesTable[i].setAttribute("onclick", "sapNewWindow(\\'" + sapHref + "\\')");
}
}
}
No, it’s not an XML document. It’s a table tag with an id named ‘orderLinesTable’. Basically, I want to collect all the ‘a’ tags in the table and check the ones that have the word ‘SAP’ then do something with them. But browsers give the ‘null’ error. That’s where I’m stuck.
hi,
because of some reasons the getElementById method returns null in Chrome and Safari and so the getElementsByTagName of null is called.
Since your code works well in FF, two cases are possible:
the table tag with an id named ‘orderLinesTable’ is created by JavaScript and some errors occur during the creation in Chrome and Safari
when the popUpSAPWindow method is called, the DOM hierarchy is not ready. In that case, call the popUpSAPWindow method when the document has been loaded (onload event)
Try to alert the document and its getElementById method in Chrome, maybe it will help to find out the problem:
function popUpSAPWindow() {
alert (document); // must be HTMLDocument
alert (document.getElementById); // function getElementById() {[native code]}
// ...
I understand what you want me to do, the problem is on the login page. Chrome and Safari don’t let me login because the table doesn’t exist in the login page, which is normal. So I put a condition that if the table with the id ‘orderLinesTable’ exist do the window pop-up for each anchor tag with the word ‘SAP’, but yet they are still giving me that error. It’s like they don’t even perform the check. (Or they don’t even bother).
Here’s the updated code:
function popUpSAPWindow() {
var orderLinesTable = document.getElementById("orderLinesTable");
var linksInOrderLinesTable = [];
[COLOR="Red"]//Here's the check[/COLOR]
if(orderLinesTable){
linksInOrderLinesTable = orderLinesTable.getElementsByTagName("a");
var linksLen = linksInOrderLinesTable.length;
for(var i = 0; i < linksLen; i++){
var sapHref = linksInOrderLinesTable[i].href;
linksInOrderLinesTable[i].setAttribute("href", "javascript:return false;");
linksInOrderLinesTable[i].setAttribute("onclick", "sapNewWindow(\\'" + sapHref + "\\')");
}
}
}
The getelementById method returns a reference to the element with the specified id or null if it does not exist. A reference to an HTML element is evaluated to true, null is evaluated to false in a condition (if statement).
So your check is OK, it means the orderLinesTable is not null in Chrome and Safari.
If you get the same error (“Uncaught TypeError: Cannot call method ‘getElementsByTagName’ of null”), i think the error message is generated by another call of the getElementsByTagName method not by the orderLinesTable.getElementsByTagName. The orderLinesTable variable cannot be null at that point in your updated source code.
@Gumape: my code works fine with the if statement. But I think it’s something to do with Chrome and Safari themselves. They have trouble with MIME Types and this is the message they give: “Resource interpreted as script but transferred with MIME type text/plain”.
It says the same to all my javascript, including jquery, jquery ui, my own javascript.
I don’t get it, since I’ve checked every script tags and they all have type=“text/javascript”. This is weird…
I added to the above HTML the function from post #8 which has the check, which you said doesn’t work, and it appears to work fine.
What problem are we dealing with now, the getElementById one or the “resource interpreted as script” one.
If it’s the resource one, that has nothing to do with your page itself. That one is a problem with your server configuration.
Your server is sending .js files as “content-type:text/plain” when it should be sending them as “content-type:application/x-javascript” instead.
@pmw57: we’re dealing with the ‘resource interpreted as script’ problem. I nearly doubted my javascript for a moment. That’s what I’ve found out as well (after hours of research), it’s the server that’s the issue. And you’ve confirmed it. Thank you very much for your help.