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');
Related
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;
});
});
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>
When the JQuery function tries to open a new page in firefox, the message "firefox prevented this site from opening a pop-up window" is presented. As I understand based on Is window,open() impossible in firefox and Links to local page do not work this is a local problem that only happens because I am trying to access a file in my server from the "localhost". However, when this site will be realy working, other people will not have the same problem just because they are not accessing their own server. Does this interpretation make sense? Or I am wrong and I have to deal with this problem? By the way, it is easy to solve locally this problem since I have only change the preferences of firefox. My worries are related with the other people accessing my web site.
For reference, this is my code:
<?php
$theUsernameDaniel = "danielcajueiro";
$theUsernameMarcelo = "marcelopapini";
?>
<html>
<head>
<meta charset="utf-8" />
<title>ControllingHiperlinks</title>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("a.peoplePage").click(function(event) {
event.preventDefault();
var theUsername = $(this).data("username");
// alert(theUsername);
// event.preventDefault();
$.post('callmpeoplepage.php', {theUsername: theUsername}, function(data) {
var thePeoplePage = window.open("about:blank");
thePeoplePage.document.write(data);
});
});
});
</script>
</head>
<body>
<a class="peoplePage" data-username="<?php echo $theUsernameDaniel ?>" href=""> Daniel Cajueiro</a>
<a class="peoplePage" data-username="<?php echo $theUsernameMarcelo ?>" href="">Marcelo Cajueiro</a>
</body>
</html>
callmpeoplepage.php is
<?php
$theUsername = $_POST['theUsername'];
echo $theUsername;
?>
You cannot open a popup except in response to a direct user action. Since you delay the window.open until the post reply finishes, it is no longer directly in response to the user's click, and therefore the popup blocker will stop it.
This will happen for everyone, and you cannot change the behavior. You could try opening the window before you submit the post, and only filling it in when the post returns - just move the window.open line up one to just before $.post
you can write like this
let win = window.open("about:blank", "_blank");
$.post('callmpeoplepage.php', {theUsername: theUsername}, function(data) {
// if data is a valid url
win.location.href = data;
});
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.
I'm building a little upload script. My form has a target to an iFrame and the iFrame locates on the same page as the form.
In the code of the iFrame I upload the files to my server. If the upload is finished i want to run a little jQuery inside the iFrame to hide a div on the parent page (where the form is located).
Strange thing is that the jQuery inside the iFrame doesnt work. If I try a simple
alert('bla bla bla'); it doesnt show.
Somebody now what i should do?
Thanks in advance!
the code of the iFrame:
<? if(isset($_POST['Q'])) { echo 'form is set';
echo $_POST['Q']; ?>
<script type="text/javascript" charset="utf-8">
// JavaScript Document
$(document).ready(function()
{
$('#test').show();
});
</script><? } ?><div id="test" style="display:none;">bla bla bla</div>
If you are using jquery withing iFrame you need to have references to JQuery libraries withing the Iframe itself.