Angie
Angie

Reputation: 899

How to create a link to another page AND specify another URL for an iframe on that page?

I have a page called dash.php and in it is an iframe (id="iframe"). On a third page I have links:

<a href="dash.php">Add</a>
<a href="dash.php">Edit</a>

I am drawing a blank on how to do this, I would appreciate a nudge in the right direction. Thank you.

Upvotes: 2

Views: 123

Answers (1)

Lio_69
Lio_69

Reputation: 110

Adding an extra parameter in your URL should fix it:

1. Main page index.php

Add -> href="dash.php?content=add"

Edit -> href="dash.php?content=edit"

2. dash.php

2.1. Grab content value:

$content = $_GET['content'];

2.2. Test it:

if($content =='add')
    $myIFrame = 'http://www.stackoverflow.com';
else
    $myIFrame = 'http://www.w3schools.com';

2.3. Display your iframe by setting its source:

src="<?= $myIFrame ?>"

As you can see, no need of Javascript here.

**

EDIT:

**

However, if you need a working JS, here it is, using the same main.php file:

var iframe;
iframe = document.getElementsByTagName('iframe')[0];

function $_GET(param) {
    var vars = {};
        window.location.href.replace( 
            /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
            function( m, key, value ) { // callback
                vars[key] = value !== undefined ? value : '';
            }
        );

    if ( param ) {
        return vars[param] ? vars[param] : null;    
    }
    return vars;
}

    if($_GET('content') == 'add')
        iframe.src="http://www.stackoverflow.com";
    else
        iframe.src="http://www.w3schools.com";

Upvotes: 1

Related Questions