简体   繁体   中英

how to change the src of the frame dynamically

I am having a "cars.html" HTML file having three frames within a frameset

<frameset id="help" rows="10%,5%,90%">

<frame id="pageTitle" frameborder=0 scrolling=no src="cars_pageTitle.html">
<frame id="pageMenu" frameborder=0 marginheight="0" src="cars_pageMenu.html">
<frame id="pageContent" frameborder=0 marginheight="0" src="#">

 </frameset>

"cars_pageMenu.html" has the menu bar
I assign this to the src of the second frame

<script type="text/javascript">
function carRequestMenu()
{
     alert(document.getElementById("pageContent"));
document.getElementById("pageContent").src='cars_requestMenuContent.html';
}
</script>

<BODY>
 <div id="pageToolbar" style="width=100%;height:10px;background-color:FF3333">
      <menu type="toolbar" style="margin-left: 0px;margin-bottom: 0px">
       <button type="button" onclick="carRequestMenu()">Request a Car</button>
       </menu>
 </div>
</BODY>

when i click the button "Request a Car" the carRequestMenu() function is called the alert shows null value.I am not able to change the src dynamically. thanks

You can use window.open and specify the target name:

    <frameset id="help" rows="10%,5%,90%">

<frame id="pageTitle" frameborder=0 scrolling=no src="cars_pageTitle.html">
<frame id="pageMenu" frameborder=0 marginheight="0" src="cars_pageMenu.html">
<frame id="pageContent" name="pageContent" frameborder=0 marginheight="0" src="#">

 </frameset>


<script type="text/javascript">
function carRequestMenu()
{
     window.open('cars_requestMenuContent.html','pageContent');
}
</script>

<BODY>
 <div id="pageToolbar" style="width=100%;height:10px;background-color:FF3333">
      <menu type="toolbar" style="margin-left: 0px;margin-bottom: 0px">
       <button type="button" onclick="carRequestMenu()">Request a Car</button>
       </menu>
 </div>
</BODY>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM