iframe src属性jquery执行两次

I'm parsing src of an iframe through jquery. As soon as I trigger an event, PDF is displayed in the iframe and gain it disappears and loading again slowly. What would be the cause for it?

<script>
function viewpdf(){ 
$('iframe').attr('src', '<?php echo $pdf;?>');
;}
viewpdf();
</script>
   <button onclick="viewpdf();">click</button>
   <iframe id="iframe"  style="width:100%;height:92.5%;"></iframe>

When the page loads viewpdf() function will execute and when I click button again pdf should be loaded into the iframe but it displays pdf suddenly and disappers and again pdf is loading. And sometimes even pdf exists in the folder am getting the error like below enter image description here

Try this..

function viewpdf(){ 
$('#iframe').attr("src", "http://www.education.gov.yk.ca/pdf/pdf-test.pdf");
}

         <button onClick="viewpdf()">click</button>
   <iframe id="iframe"  style="width:100%;height:92.5%;"></iframe>
      </div>

I think to achieve what you said in the comment, your code is not a good way. Do it like this...

<script>
$(document).ready(function(){

    $('iframe').attr('src', '<?php echo $pdf;?>');


    $("#close").click(function(){
    $("iframe").hide();
    });


    $("#open").click(function(){
    $("iframe").show();
    });

});
</script>

You should not load the PDF again and again if you are not changing the src.