最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getElementById returns null or undefined in Safari - Stack Overflow

programmeradmin0浏览0评论

Below is a code snippet of some frames I am trying to traverse using the DOM in javascript. I can use document.getElementById('raiseConfirm').click() in FF and IE. It will not work in safari. I have tried a multitude of different things to try and get to the anchor with the ID raiseConfirm. It looks as if document.getElementById() in this case doesn't find any of the elements in this frame. I have tried window.document, self.document, contentWindow,contentDocument, I have tried naming the frame like this, window.frames['gvdd'].document, and when I test this, the document is an object and has things in it, but as soon as I try to use the function getElementById() on it, it returns undefined or null.

Can anyone point me to the correct solution?

  <form method=​"POST" 
        action=​"somescript.asp?CMD=POST&​TODO=TRUE2" 
        id=​"FORMSV2DISPLAY"
        name=​"FORMSV2DISPLAY">​
    <a id=​"raiseConfirm" href=​"#divConfirm" class=​"nyroModal">HERE IT IS​</a> 
    <div id="divConfirm" style=​"display:​none;​">​…​</div>​ 

Below is a code snippet of some frames I am trying to traverse using the DOM in javascript. I can use document.getElementById('raiseConfirm').click() in FF and IE. It will not work in safari. I have tried a multitude of different things to try and get to the anchor with the ID raiseConfirm. It looks as if document.getElementById() in this case doesn't find any of the elements in this frame. I have tried window.document, self.document, contentWindow,contentDocument, I have tried naming the frame like this, window.frames['gvdd'].document, and when I test this, the document is an object and has things in it, but as soon as I try to use the function getElementById() on it, it returns undefined or null.

Can anyone point me to the correct solution?

  <form method=​"POST" 
        action=​"somescript.asp?CMD=POST&​TODO=TRUE2" 
        id=​"FORMSV2DISPLAY"
        name=​"FORMSV2DISPLAY">​
    <a id=​"raiseConfirm" href=​"#divConfirm" class=​"nyroModal">HERE IT IS​</a> 
    <div id="divConfirm" style=​"display:​none;​">​…​</div>​ 
Share Improve this question edited May 25, 2012 at 14:57 Sampson 269k76 gold badges545 silver badges568 bronze badges asked May 25, 2012 at 14:54 user1417467user1417467 851 silver badge6 bronze badges 2
  • 2 Are you sure the frame has finished loading by the time your script runs (i.e. is your code located in a load handler)? – Frédéric Hamidi Commented May 25, 2012 at 14:59
  • This does not describe your frame structure, which would be required to answer the question. – Diodeus - James MacFarlane Commented May 25, 2012 at 15:00
Add a ment  | 

4 Answers 4

Reset to default 1

You could use jquery:

$('#raiseConfirm')

It will surely work on all browsers. That's the magic of jquery :)

And if you want to attach to the click event:

$(document).ready(function () {
    $("#raiseConfirm").click(doSomenthing);
});

var doSomenthing = function () {
    /* Something I do here */
}

I tried to say this before and it posted weirdly. I was able to fix this problem using JQuery and this is what I did.

      $("#raiseConfirm").trigger("click");

document.getElementById() will not work in safari. Instead you need to use document.all... Therefore, JQuery es into picture where it is supported by all browsers. This can be the major difference between Javascript and JQuery where to be used.

I tried this in JSFiddle. I couldn't get it to work in Safari without jQuery.

http://jsfiddle/belthasar/Zct4S/

发布评论

评论列表(0)

  1. 暂无评论