.NET Framework Bookmark and Share   
 index > JScript for the .NET Framework > How to access Element from page loaded inside Iframe, where My page is containing that iframe Tag...Please Help
 

How to access Element from page loaded inside Iframe, where My page is containing that iframe Tag...Please Help

Here Is the Problem..............

file1.html

<html>

<body>

<input id="Text1" type="text" />

</body>

<html>

file2.html

<html>

<body>

<iframe src="file1.html"></iframe>

<script>

Here How i will access the textbox Text1 here?

<script>

<body>

<html>

Somnath Panja

As long as the document loaded in the IFrame isfrom thesame domain as the parent frame, then you should be able to access it's contents.

Sample is

Code Snippet
<script>
document.frames["MyIFrameID"].document.MyFormID.MyInputElem
</script>

Of course, you need to replace "MyIFrameID" with the id of your IFrame.IfIFrame's document is notsame domain as the parent frame's domain then you willget access denied security exception.

With the little modification to your code this can be achieved. Below is the modified code

File1.html

Code Snippet

<html>

<body>

<form id="MyForm">

<input id="Text1" type="text" value="Old value" />

<form>

</body>

</html>

File2.html

Code Snippet

<html>

<body>

<iframe id="iframe" src="File1.htm"></iframe>

<input id="but" type="button" onclick="SetText();" />

<script type="text/jscript">

function SetText()

{

document.frames["iframe"].document.forms["MyForm"].Text1.value = "My new Value";

}

</script>

</body>

</html>

Hope with helps.

Ramakrishna Neela

As long as the document loaded in the IFrame isfrom thesame domain as the parent frame, then you should be able to access it's contents.

Sample is

Code Snippet
<script>
document.frames["MyIFrameID"].document.MyFormID.MyInputElem
</script>

Of course, you need to replace "MyIFrameID" with the id of your IFrame.IfIFrame's document is notsame domain as the parent frame's domain then you willget access denied security exception.

With the little modification to your code this can be achieved. Below is the modified code

File1.html

Code Snippet

<html>

<body>

<form id="MyForm">

<input id="Text1" type="text" value="Old value" />

<form>

</body>

</html>

File2.html

Code Snippet

<html>

<body>

<iframe id="iframe" src="File1.htm"></iframe>

<input id="but" type="button" onclick="SetText();" />

<script type="text/jscript">

function SetText()

{

document.frames["iframe"].document.forms["MyForm"].Text1.value = "My new Value";

}

</script>

</body>

</html>

Hope with helps.

Ramakrishna Neela

Thank you So much...

Somnath Panja
hello!

reviving a topic =D

it only worked in IE.
someone knows how to do this in FF.

thanks
coutinhovr
hello!

reviving a topic =D

it only worked in IE.
someone knows how to do this in FF.

thanks

Here is a simple function that checks which refer type for iFrame content window is true and returns the correct:

function getFrameWindow(frameId) {
  var frame = document.getElementById(frameId);
  var result = null;
  if (frame.contentDocument) {
    // For NS6
    result = frame.contentDocument.window || frame.contentDocument.defaultView; 
  } else if (frame.contentWindow) {
    // For IE5.5 and IE6
    result = frame.contentWindow;
  } else if (frame.document) {
    // For IE5
    result = frame.document.window;
  }
  return result;
}

// example
//getFrameWindow('iFrame1').document.getElementById('Submit').onclick();

Regards,

M.Zografski
  • Proposed As Answer bycoutinhovr Saturday, September 12, 2009 6:53 PM
  •  
M.Zografski
Thank you. =)
coutinhovr

You can use google to search for other answers

Custom Search

More Threads

• Help needed - Problem in getting value in asp.net 1.1 with Anthem manager
• How to enable C# DatePicker after validating a date field in javascript
• Alertbox title
• How to Disply image in HTML from from SQL server
• winForm User Control and JavaScript
• How to implement hotkeys functionality in .Net for pdf document?
• how to install suport for jscript into visual web developer 2008 express edition?
• To find the location in google map by submit the IP of the client in Textbox
• Max Javascript Array Index ?
• How to pass listbox from GridView's footer row to javascript