最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change frame source without reloading the page - Stack Overflow

programmeradmin0浏览0评论

I am creating a Template Site, i want to change the content of the IFrame based on the link clicked in javascript, but the error i get is if i change the 'src' attribute of the iframe the page gets reload.

So my question is

how to change iframe content without reloading the Page? below is my code

// HTML part

<li class="inside first">
   <a href='JavaScript:void(0);' onclick='clicked(0);' title="HOME">HOME</a>
</li>
<li class="inside">
   <a  href=''onclick='clicked(1); ' title="PRODUCTS">PRODUCTS</a>
</li>
<li class="inside">
   <a  href='' onclick='clicked(2); ' title="SOLUTIONS">SOLUTIONS</a>
</li>
<li class="inside">
    <a  href=''  onclick='clicked(3); 'title="SERVICES">SERVICES</a>
</li>
<li class="inside">
    <a  href='' onclick='clicked(4); ' title="SUPPORT">SUPPORT</a>
</li>

<div id="mainContent">
    <iframe 
           src=''
           width="100%"
           height="100%"
           name='content'
           id='content'>&nbsp;
    </iframe>
</div>

//Javascript part

<script type="text/javascript">
   var index=0;
   var link_list= new Array('homepage.html','product.html','solution.html',
            'services.html','support.html','event.html',
             'partner.html','pany.html');

   var frame=document.getElementById('content');

   frame.src=link_list[index];

   function clicked(key)
   {

       // The following line is redirecting the page
       frame.src=link_list[key];
       return false;
   }
   </script>

I am creating a Template Site, i want to change the content of the IFrame based on the link clicked in javascript, but the error i get is if i change the 'src' attribute of the iframe the page gets reload.

So my question is

how to change iframe content without reloading the Page? below is my code

// HTML part

<li class="inside first">
   <a href='JavaScript:void(0);' onclick='clicked(0);' title="HOME">HOME</a>
</li>
<li class="inside">
   <a  href=''onclick='clicked(1); ' title="PRODUCTS">PRODUCTS</a>
</li>
<li class="inside">
   <a  href='' onclick='clicked(2); ' title="SOLUTIONS">SOLUTIONS</a>
</li>
<li class="inside">
    <a  href=''  onclick='clicked(3); 'title="SERVICES">SERVICES</a>
</li>
<li class="inside">
    <a  href='' onclick='clicked(4); ' title="SUPPORT">SUPPORT</a>
</li>

<div id="mainContent">
    <iframe 
           src=''
           width="100%"
           height="100%"
           name='content'
           id='content'>&nbsp;
    </iframe>
</div>

//Javascript part

<script type="text/javascript">
   var index=0;
   var link_list= new Array('homepage.html','product.html','solution.html',
            'services.html','support.html','event.html',
             'partner.html','pany.html');

   var frame=document.getElementById('content');

   frame.src=link_list[index];

   function clicked(key)
   {

       // The following line is redirecting the page
       frame.src=link_list[key];
       return false;
   }
   </script>
Share Improve this question edited Mar 15, 2012 at 12:15 Naveen Kumar asked Mar 15, 2012 at 8:12 Naveen KumarNaveen Kumar 4,6011 gold badge20 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Just write hrefs for those links and add attribute target="content".

This can be done w/o javascript at all.

Your link_list is wrong, JavaScript is case-sensitive, it's new Array(), not new array():

var link_list = new Array(...);

Use the array literal [] and you're fine:

var link_list = ['homepage.html','product.html','solution.html',
            'services.html','support.html','event.html',
             'partner.html','pany.html'];

If you check your browser's JavaScript error console it's easy to find such errors:

[09:16:09.208] array is not defined @ file:///.../temp.html:34
发布评论

评论列表(0)

  1. 暂无评论