Problem writing a script block to an IFrame

Hello,

I need to dynamically write a script block to an IFrame. Unfortunately the script block is not executed. Here is the code: The first document.write works as expected, but the "Hello form Script" message never appears. Does anybody know what's wrong with my code?

Thank you for you help!


<HTML>
      <HEAD>
            <script id="clientEventHandlersJS" language="javascript">
<!--

function Button1_onclick() {
      var myIFrameDoc=IFrame.document;
            
      myIFrameDoc.designMode="on";
      myIFrameDoc.open();
      myIFrameDoc.write("Hello");
      myIFrameDoc.write("<script>alert('Hello from Script');</script>");            
      myIFrameDoc.close();                        
}

//-->
            </script>
      </HEAD>
      <body>
            <P>
                  <iframe id="IFrame" style="WIDTH:400px;HEIGHT:300px"></iframe></P>
            <P>
                  <INPUT id="Button1" type="button" value="Button" name="Button1" language="javascript" onclick="return Button1_onclick()">
            </P>
      </body>
</HTML>
falleraiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
fallerai,
Why dont you eliminate document.write() within the alert() function?
Just use alert() functions to execute alert() message.

Instead of
myIFrameDoc.write("<script>alert('Hello from Script');</script>");  
Change to
alert('Hello from Script');

Regards
x_com  
0
ahosangFinance Systems DeveloperCommented:
The two successive calls to document.write is the problem. The best way to do this sort of thing is to build up a string and then write it all at once.
function Button1_onclick() {
     var myIFrameDoc=IFrame.document;
         
     myIFrameDoc.designMode="on";
     var str="Hello";
     str+="<sc"+"ript>alert('Hello from Script');</sc"+"ript>";          
     myIFrameDoc.write(str);
     myIFrameDoc.close();                    
}
0
ahosangFinance Systems DeveloperCommented:
Some of what I said wasn't right. But in any case, the code should work. Always break up <script> tags in javascript strings.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

falleraiAuthor Commented:
Hello,

thank you for your comments so far. Unfortunately I still could not solve the problem

@x_com: The alert message embedded in the script block is just an example. This can be replaced with any other Java Script.

@ahosang: I changed the code as you habe suggested, but the Messagebox still does not pop up.
0
David H.H.LeeCommented:
fallerai ,
Just want to mention somethings here, if you want to output somethings, document.write(0 should do the trick. But, if you want to pop up any alert() message, why don't you just use build in messagebox function?
>>This can be replaced with any other Java Script.
-now you're functions already in javascript....
0
David H.H.LeeCommented:
If you only want to pop up a message, try this :
myIFrameDoc.write("<script>alert('Hello from Script');</script>");      
change to
myIFrameDoc.write(alert('Hello from Script'));      
-but after click ok, you will get 'undefine" items, simply remove it afterward.
0
falleraiAuthor Commented:
Some more background:

When I split up the above code in two html-pages and load the IFrame with the help of the scr-Attribute then it works as expected: A page with an IFrame displayed the Hello string is displayed and the Messagebox pops up.

PageWithIFrame.htm:

<HTML>
      <body>
            <iframe id="IFrame" src="IFrameS.htm" style="WIDTH:400px;HEIGHT:300px"></iframe>
      </body>
</HTML>

IFrameContent.htm:

<HTML>
      <body>
            Hello
            <script>alert('Hello from Script');</script>
      </body>
</HTML>

But when I load the content dynamically to the IFrame like I posted in my question the Script block ist not executed. The Messagebox is not shown.

The content which is loaded in the IFrame will be a complex html page wich includes lot of JavaScript. I need to load the content dynamically to the IFrame, because it is not known in advance. Another solution would be to gernerate a html-page on the fly and refernece it with the IFrame src attribute. But I think this is not possible on the client side. I can not use server side functionality for this solution.
0
falleraiAuthor Commented:
There is a mistake in the example above

It sould look like this:

PageWithIFrame.htm:

<HTML>
     <body>
          <iframe id="IFrame" src="IFrameContent.htm" style="WIDTH:400px;HEIGHT:300px"></iframe>
     </body>
</HTML>

IFrameContent.htm:

<HTML>
     <body>
          Hello
          <script>alert('Hello from Script');</script>
     </body>
</HTML>
0
falleraiAuthor Commented:
I finally found the solution :) and want to share it with you. The problem was the line myIFrameDoc.designMode="on";

I thought it is necessary to set this Attribute to make it possible to write content  to the IFrame. But it is actually not necessary. Moreover it prevents the execution of JavaScript.

Since I removed that line everything works as expected:

Final solution:

<HTML>
     <HEAD>
          <script id="clientEventHandlersJS" language="javascript">
<!--

function Button1_onclick() {
     var myIFrameDoc=IFrame.document;
         
     myIFrameDoc.open();
     myIFrameDoc.write("Hello");
     myIFrameDoc.write("<script>alert('Hello from Script');</script>");          
     myIFrameDoc.close();                    
}

//-->
          </script>
     </HEAD>
     <body>
          <P>
               <iframe id="IFrame" style="WIDTH:400px;HEIGHT:300px"></iframe></P>
          <P>
               <INPUT id="Button1" type="button" value="Button" name="Button1" language="javascript" onclick="return Button1_onclick()">
          </P>
     </body>
</HTML>
0
falleraiAuthor Commented:
I am a new user of this site. How can I close this Q/A. I can not find how to do this.
0
VincentPugliaCommented:
works for me  (i changed the Iframe id, probably not necessary, but...

function Button1_onclick() {
     theFrame.document.write("<html><script>function doit(){alert('Hello from Script');} window.onfocus= doit()</script><body>Hello</body><script>alert(1)</script></html>")
}
<iframe id="theFrame" style="WIDTH:400px;HEIGHT:300px"></iframe></P>

there is a difference between
document.write
and
IFrameDoc.write

Vinny
0
David H.H.LeeCommented:
fallerai,
I got it, simply remove myIFrameDoc.designMode="on".
That's i found it. It pretty easy too.
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David H.H.LeeCommented:
oppps, late response. Cache problems.......:-(
No luck!
0
David H.H.LeeCommented:
fallerai ,
You can post a thread at CS area. Ask the admin to close the question with Points Refund.
In that thread, add refer :
http://oldlook.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20781542.html

Regards
x_com
0
VincentPugliaCommented:
For those who are still reading

interestingly enough, the onload event handler does not exectue -- possibly because the page isn't 'loaded'

function Button1_onclick()
{
theFrame.document.write("<html><script>function doit(){alert('Hello from Script');} </script> <body onload= doit()>Hello</body> <script>alert(1)</script></html>")

  var myIFrameDoc=theFrame.document;
  myIFrameDoc.designMode="on";
   
}
0
falleraiAuthor Commented:
@x_com I seems we have found the solution in just the same moment :) But insted of refunding the points you will get some :) Thank you for you help!!!
0
David H.H.LeeCommented:
fallerai , thanks...still got luck..

Regards
x_com
0
VincentPugliaCommented:
fallerai -- if you still want the designMode, look at my solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.