iframe src attribute jquery execution twice - php

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

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.

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>

Related

Change a URL when you click on it

In my index.html there is a URL with an image.
So I want when you click on the image URL, the URL behind the image is permanent changed.
I tried it with GET and POST but I can't find a solution.
Would be nice if someone gives me an answer.
try this
script to be added
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
html code:
<img src="some_img.png" class="menulink" id="bg"/>
jquery code:
$(function() {
$('.menulink').click(function(){
$("#bg").attr('src',"img/picture1.jpg");
return false;
});
});

How to auto reload an iFrame without blinking in jQuery every second?

I would like to auto reload an iFrame that is in my webpage called "dispatch1.php" every second without having it constantly blinking. Here is my line of HTML code with the iFrame.
<iframe id="dispatch1" src="table.php" name="dispatch1" width="800px" height="400px" style="margin-right:10%;margin-top:10%;"></iframe>
This is the code that I have so far with the jQuery, but it isn't working.
<script>
$(function() {
startRefresh();
});
function startRefresh() {
setTimeout(startRefresh,1000);
$.get('table.php', function(data) {
$('#dispatch1').html(data);
});
}
</script>
Edit: I have fixed the problem. I just remove the code from the iFrame and put it into the dispatch1.php file.

how to remove first div from a html page include by iframe using jquery

I add a html page in my CI project using iframe. Now i want to remove first div from included page. there is no id for that div. i write a jquery code but it removes whole iframe. here it is
$(document).ready(function(){
$("iframe").load(function(){
//$('div:first').remove();
$("body").find("div:first-child").remove();
});
});
html
<div class="span12">
<iframe id="patrolIframe" width="100%" height="900" frameborder="0" scrolling="yes" allowtransparency="true"
src="http://...../map.html"></iframe>
</div>
$(document).ready(function(){
$("iframe").load(function(){
$('div')[0].remove();
});
});
try
$(document).ready(function(){
$("iframe").load(function(){
$("body",this).find("div:first-child").remove();
});
});
Try
$(document).ready(function(){
$("iframe").load(function(){
//$('div:first').remove();
$(this).find("div:first-child").remove();
});
});
You $("body") selector selects the body of the page your jquery is loaded in, that is, the whole page. Im guessing your iframe is inside the first div of that page, so it gets removed. I dont use iframes, but i assume the $(this) on that case is refering to the iframe so it should work.

How to call a Javascript function when clicking on a pdf document loaded in a webpage?

I need to invoke a javascript function when I click on a pdf document loaded in a webpage. The code which I have written as follows. But it is not working.
<script language="javascript" type="text/javascript">
function buttonEvent(msg){
alert(msg);
}
</script>
<embed src="sample.pdf" id="buttonEmbed" width="500" height="600" type="application/pdf"
onClick="buttonEvent('buttonClicked')"/>
The PDF is loaded with a plugin and is not part of your DOM model. That's why listening event handlers other than onload for the embed node is not possible.
Alternative solution
You could add a button display more at the top or bottom of the PDF outside the embed container. Add your click event to this button.

Dynamically figure source of iFrame with jQuery

I'm trying to create a program on my server that enables me to pull information from different websites. It's already written in PHP and works perfectly. I gather this information from an iframe on one of my pages. When I use the original page that was loaded by the php script in the iframe, everything works fine. But, if I click on a link inside of the iframe, the src is different and I don't know how to access it. How do I figure out the NEW src of the <iframe> with jQuery? I searched this site and can't find a good answer to this. Below is an example of my code:
the PHP:
<?php
$link = $_POST['link'];
?>
<div id="button">Button</div>
<iframe src="<? echo $link;?>"></iframe>
<script type="text/javascript" src="something.com/jquery.js"></script>
<script type="text/javascript">
$('#button').click(function()
{
//alert iframe src
});
</script>
EDIT:
I current have this running, and it only alerts the original src when I click a link in the <iframe>. I'm needing it to alert the NEW src of the <iframe>.
<iframe id="blog_iframe" onload="iframeLoader();" height="600px" width="98%" src="<? echo $url;?>" scrolling="auto">
<p>Your current browser does not support iFrames. Please use a different browser</p>
</iframe>
<script type="text/javascript">
function iframeLoader()
{
alert($('iframe').attr("src"));
}
</script>
Use .attr()
$('#button').click(function() {
//alert iframe src
alert( $("iframe").attr("src") );
});
** EDIT **
According to Edited question.
You're actually trying to get src of <a> tag inside of <iframe> tag
Try this
alert( $("iframe").contents().find("a").attr("href") );
var iFrameSrc = $("iframe").attr('src');

Categories