Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set frame src using Javascript

I have a HTML element

<frame src="#" title="Content Frame" name="content" id="content" />

I want to set it's "src" dynamically using Javascript on page load. How can I do that ?

I am trying something like;

document.getElementById('content2').contentWindow.location = 'xyz_frame.html';

But for some reason it is not working..Again it is a element and not

A Rough code;

<html>
<head>
<script language="javascript"> 
function LoadPage(){ 
document.getElementById('content2').src = 'ipad_lrd_frame.html';
}
</script> 
</head>
<body onload="LoadPage()">
<frame id="content2"></frame>
</body>

</html>
like image 874
copenndthagen Avatar asked Apr 30 '11 13:04

copenndthagen


2 Answers

This should work;

document.getElementById('content2').src = "url";

(Also you have mismatched IDs for the frame and getElementById call)

For a FRAME, you need a FRAMESET which precludes the use of a BODY, so;

<frameset rows="50%,*" onload="LoadPage();">    
   <frame id="content2"></frame>
   .....

Update:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<script type="text/javascript">
function LoadPage(){
    document.getElementById('content1').src = "http://www.google.com";
    document.getElementById('content2').src = "http://www.bing.com";
}
</script>
<title></title>
</head>
<frameset rows="50%,*" onload="LoadPage();">    
    <frame src="#" id="content1">    
    <frame src="#" id="content2">                
</frameset>
</html>
like image 66
Alex K. Avatar answered Sep 19 '22 10:09

Alex K.


I would think fetching frames by name would be more suitable:

document.getElementsByName('content2')[0].src = 'ipad_lrd_frame.html';

Tested it successfully. Ensure your page uses a frameset doctype and you do not include body tags (use frameset tags instead). Also note your script type should be text/javascript, rather than language "javascript".

like image 28
lpd Avatar answered Sep 18 '22 10:09

lpd