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.