开发者

navigating to a link from inside iframe

I hope someone here will be able to help, I thank i advance.

I have a Iframe (in my domain), when someone clicks a link in the iframe i dont want that link to open automatically, I need it to go to a function that will navigate to that link after adding some things to the page.

I use this function to fill the iframe

function setIframeHtml(html) {

   var iframe = document.getElementById("myFrame");
   var doc = iframe.document;
   if (iframe.contentDocument) {
       doc = iframe.contentDocument; // For NS6
   }
   else if (ifra开发者_如何转开发me.contentWindow) {
       doc = iframe.contentWindow.document;  // For IE5.5 and IE6
   }
   // Put the content in the iframe
   doc.open();
   doc.writeln(html);
   doc.close();

   navigating = false;
}

The html parameter the function gets is the page to be loaded after adding my things.

When i do this from inside the iframe i get an error saying

Cannot read property 'document' of null    

only when i call that function from out side if the iframe, does it work.

Any ideas how to get this working?


I used jQuery for this, there is probably a method without using jQuery but unfortunately I don't know it.

You can assign code to be run when a user clicks a link, the code will be run, and then the frame will go to the page specified by the link.

Here is an example with an alert box.

$("a").click(function() {
    //do whatever you need to do on a link press here.    
    alert("Test");
});

In action: http://jsfiddle.net/W2NMu/, Note how the frame does not change until the alert box is closed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜