1

I have this code which returns on a click of a button the URL of an iframe

<html>
<body>
<iframe src="start.php" name="vframe" id="vframe"></iframe>

<script>
function glink() {
alert(document.getElementById('vframe').contentWindow.location.href);
}

</script>

<button onclick="glink()">Click me</button>
</body>
</html>

What I want to do is somehow use the javascript function to create a variable (the link it captures via the onclick) inside the function without the need for an onclick that can be used in the parent page

For example

<html>
<body>
<iframe src="start.php" name="vframe" id="vframe"></iframe>

<script>
function glink() {
var x = document.getElementById('vframe').contentWindow.location.href;
}

</script>


Then I can use var x in this way ...

<script> if ($link =="start.php") { echo '<img src="start.jpg">' } else {
   echo '<img src="end.jpg">' }
 </script>
</body>
</html>

Obviously, the javascript is somehow able to get the link, but I need to get it programmatically and not have to rely on a 'click', and I need to be able to hold the link in a variable so I can use it in a conditional statement, as noted above.

I am not too familiar with javascript syntax.

====

Edited

====

I tried this (based on DevishOne's suggestion):

<html>
<body>
<iframe src="start.php" id="vframe"></iframe>

<script>
function glink() {
    var x = $('#vframe').attr('src');
    $.get('processIframeLink.php',{link:x});
}
</script>

<?
$link = $_REQUEST['link'];
echo $link;
if ($link =="start.php") {
    echo '1';
} else {
    echo '2';
}
?>

</body>
</html>

But there is nothing showing at '$_REQUEST['link'];'

however I just tried this:

<html>
<body>

<? $test = '<p id="demo"></p>'; 
echo $test;

?>

<iframe src="start.php" name="vframe" id="vframe"></iframe>
<script>
document.getElementById("demo").innerHTML = document.getElementById('vframe').contentWindow.location.href;
</script>
</body>
</html>

Here, echo $test returns the result of the script. But for some reason, it keeps returning "about:blank"

If there was some way to get "document.getElementById('vframe').contentWindow.location.href;" to show the iframe link, then I have accomplished the goal.

I have found "document.getElementById('vframe').contentWindow.location.href;" in many places on the net saying it can get the iframe link, but I keep getting "about:blank"

==========

edit

Here is the latest attempt:

<html>
<body>
<iframe src="start.php"></iframe>
 <script>
function glink() {
 var x = frames[0].location;
 document.getElementById("demo").innerHTML = x;
}
</script>

<button onclick="glink()">Click me</button>

<? $link = '<p id="demo"></p>'; ?>
   echo '<p id="demo"></p>';
?>     
</body>
</html>

What this does is (upon the click), allows the JS to find the link and then the PHP reads it as a variable. The only issue here is if there is no 'click', frames[0].location; = "about:blank" ... if there is a click, then the code writes the link to the PHP variable.

how to 'read' the link without the click or to simulate a click to bring in the link, that would be ideal.

user3697638
  • 83
  • 1
  • 1
  • 7

2 Answers2

1

The problem with your question is that JS cannot directly communicate with PHP.

PHP page is rendered instantly, and JS is rendered WHILE page is being loaded, through your Web browser.

To solve this problem you need to use either Ajax or jQuery and use POST or GET functions through POSTBACK functions.

They allow you to send information to secondary page, while fetching results and rendering those results into the primary page.

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • my example above shows I can use the JS to give me a PHP variable (see code above, try it yourself). The only problem is "document.getElementById('vframe').contentWindow.location.href;" keeps returning "about:blank", and not giving me the link as it is alleged to be able to do. the trick now is to find some javascript that can get the iframe link, now that I know how to read it into a PHP variable. the problem is there doesn't seem to be code that will grab the link that is not some sort of 'onclick' thing. thanks for the reply. – user3697638 Sep 06 '14 at 08:06
  • ok, I did another google for you. So you are trying to grab the SRC parameter of the iFrame? Just to be sure. – HelpNeeder Sep 06 '14 at 08:11
  • if is, then the POSTBACK function should return the value of this '$('iframeId').contents().get(0).location.href'. It will grab the content of the URL that the frame is working with. I did not test it, but seems legit from here http://stackoverflow.com/questions/748727/getting-the-current-src-of-an-iframe-using-jquery – HelpNeeder Sep 06 '14 at 08:14
  • yes, the URL of the iframe (all locally served). so when the server navigates inside the iframe, the parent page has the code that picks up which page is being shown in the iframe. ... this code gets that done, ( untitled Get iframe location. ) but I have to click a button to get it. i need *it* to be *got* without having to click something ... thanks for the reply – user3697638 Sep 06 '14 at 08:17
  • so you are aiming to get part of the code that is being rendered withing 'alert(frames[0].location);'? – HelpNeeder Sep 06 '14 at 08:19
  • if so, then you COULD (i have no idea if this would work) use jQuery '<$('iframe').attr('onclick')' but i can suspect that could resuslt with simply resulting 'alert(frames[0].location);'. I don't want to be hacky, but if you could render a page into iFrame, then create automatic click and somehow recover the result.... This IS HACKY, and i will not help with lol besides cross-site scripting limitations. – HelpNeeder Sep 06 '14 at 08:23
  • yes, the code from the link you gave me does not render the iframe link. My little code snip does so, perfectly, but the only problem is you have to 'click' something. Somehow, the JS 'knows' what the link is, otherwise the click would show nothing. so since the link is 'known' by the JS (theoretically) *prior to* the 'click', my guess is the data that pops up in the alert box on the 'click' is stored somewhere somehow that can be culled somehow from the JS to show like this: var x = frames[0].location; document.getElementById("demo").innerHTML = x; then

    will give it to me
    – user3697638 Sep 06 '14 at 08:31
  • but, the click result will be known only by the browser. – HelpNeeder Sep 06 '14 at 08:33
  • in theory how to render that information prior to clicking is above my head lol – HelpNeeder Sep 06 '14 at 08:34
  • ... but without the 'click', "var x = frames[0].location;" = "about:blank" --- why you have to click something to get the data and not just be a way to get it without a click is the mystery I am trying to solve ... – user3697638 Sep 06 '14 at 08:35
  • you can't. http://stackoverflow.com/questions/22106192/jquery-onclick-before-jquery-click ... but ill search more – HelpNeeder Sep 06 '14 at 08:40
0

HTML

<script>
function glink() {
    var x = $('#vframe').attr('src');
    $.get('processIframeLink.php',{link:x});
}
</script>

PHP (processIframeLink.php)

$link = $_REQUEST['link'];
if ($link =="start.php") {
    echo '<img src="start.jpg">';
} else {
    echo '<img src="end.jpg">';
}
DevlshOne
  • 8,357
  • 1
  • 29
  • 37