SunQuest
           JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2006, 06:09 AM
doubledipped doubledipped is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 10 doubledipped User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 36 m 50 sec
Reputation Power: 0
Angry Trouble getting events to fire in an iframe

I have a CMS system that uses an HTML widgeditor.
When a div with a correct class is clicked a floating div pops up where the user can edit the html etc.
This div uses an iframe and there is an issue with IE that won't allow relative links to be added to iframe.
http://www.thescripts.com/forum/thread89029.html
http://freetextbox.com/forums/thread/4977.aspx

I need the ability to add relative links, the cms sys is also used by our admin guys to set up html/text etc for other sites from this site. Anyway I used the example in that link and came up with a function to replace the old .innerHTML call but now the events that were firing (keydown,mouseup,keyup) are not firing in IE.

So I have tried writing the event handlers inline to this function so that they are included into the iframe however they are still not firing. If I take the html for the iframe out and put it in a page by itself it works fine so why isn't it working when its in the iframe.

Any help would be much appreciated.

The function that replaces the innerHTML call is:

var iframeTemplate = "\
<html>\
<head>\
<link href=\"INSERT:STYLESHEET:END\" rel=\"stylesheet\" type=\"text/css\" />\
</head>\
<body id=\"iframeBody\">\
INSERT:CONTENT:END\
</body>\
INSERT:EVENTS:END\
</html>\
";

var iframeEvents = "\n<script type=\"text/javascript\">\n"
iframeEvents += "<!--\n"
iframeEvents += "var myElem = document.getElementById('iframeBody');\n"
//iframeEvents += "var myElem = document.getElementsByTagName('body')[0] ;\n"
iframeEvents += "if (myElem.attachEvent)\n"
iframeEvents += "{\n"
iframeEvents += "myElem.attachEvent(\"onmouseup\", testEvent);\n"
iframeEvents += "myElem.attachEvent(\"onkeyup\", testEvent);\n"
iframeEvents += "myElem.attachEvent(\"onkeydown\", testEvent);\n"
iframeEvents += "}\n"
iframeEvents += "else\n"
iframeEvents += "{\n"
iframeEvents += "myElem.addEventListener(\"mouseup\", testEvent, false);\n"
iframeEvents += "myElem.addEventListener(\"keyup\", testEvent, false);\n"
iframeEvents += "myElem.addEventListener(\"keydown\", testEvent, false);\n"
iframeEvents += "}\n"
iframeEvents += "function testEvent(){alert('event firing!')};\n"
iframeEvents += "//-->\n"
iframeEvents += "</script>\n"

/*
**** Use this function instead of innerHTML as in IE iframes dont support relative links ******
*/
function CopyHtmlToIframe(iframe, HTML){

var documentTemplate = iframeTemplate

/* Insert dynamic variables/content into document */
documentTemplate = documentTemplate.replace(/INSERT:STYLESHEET:END/, widgStylesheet);
documentTemplate = documentTemplate.replace(/INSERT:CONTENT:END/, HTML);
documentTemplate = documentTemplate.replace(/INSERT:EVENTS:END/, iframeEvents);

iframe.contentWindow.document.open();
iframe.contentWindow.document.write(documentTemplate);
iframe.contentWindow.document.close();
}

If I take the rendered html out and put it in a file by itself like the following it works fine:

<html>
<head>
<link href="/jobboard/widgeditor/css/widgContent.css" rel="stylesheet" type="text/css" />
</head>
<body id="iframeBody">
<P>Praesent sed nibh eget justo tristique commodo. Phasellus justo dolor, vulputate nec, mollis ac, tempus et, orci. Praesent et justo eget quam aliquam tristique. Nam vitae mi sit amet odio viverra aliquet. Nunc eleifend felis in leo. Vivamus rhoncus turpis ut lorem. Phasellus sollicitudin, pede quis lobortis laoreet, pede nibh elementum nunc, vel pellentesque turpis dui et felis. Cras lorem nulla, vulputate condimentum, pellentesque id, mattis eget, ipsum. Proin sit amet elit. Aliquam aliquet pede at ipsum. Sed malesuada condimentum nulla. Praesent urna est, feugiat ac, pellentesque nec, condimentum non, lacus. Praesent congue vestibulum libero. Quisque non pede. Sed eu tellus nec nulla ultricies ornare.</P>
</body>
<script type="text/javascript">
<!--
var myElem = document.getElementById('iframeBody');
if (myElem.attachEvent)
{
myElem.attachEvent("onmouseup", function(){testEvent(); return true;});
myElem.attachEvent("onkeyup", function(){testEvent(); return true;});
myElem.attachEvent("onkeydown", function(e){testEvent(); return true;}, false);
}
else
{
myElem.addEventListener("mouseup", function(){testEvent(); return true;}, false);
myElem.addEventListener("keyup", function(){testEvent(); return true;}, false);
myElem.addEventListener("keydown", function(e){testEvent(); return true;}, false);
}
function testEvent(){
alert('event firing!')
};
//-->
</script>
</html>


I am hoping those event handlers will call functions that are declared in the document that includes the iframe but at the moment I can't even get this test function to fire.

Thanks in advance for any help or advice Rob

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Trouble getting events to fire in an iframe


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway