最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opening different iframe depending on different button click - Stack Overflow

programmeradmin1浏览0评论

i am trying to open different iframe windows by assigning iframe src to a variable , when the page initially loads it should show only two buttons on the page , when i click on the button , an iframe should get loaded depending on which button is pressed , i tried doing something like this to create buttons and creating iframes , but it is not working

<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="whatnext">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x = "Hello!";
function showX() {
    var whatnext;
    if b1.onclick==true
        whatnext= "/" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;"
        elseif b2.onclick=true
        whatnext= "/" style="border: 0px none; height: 555px; margin-left: -116px; margin-top: -25px; width: 330px;"
}
</script>
and another piece of trial is
<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="x" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x ;
function showX() {
    src="https:";
}
b1.onclick = function() {
    x = ";
    showX();
};
b2.onclick = function() {
    x = "www.sitepoint";
    showX();
};
</script>

i am trying to open different iframe windows by assigning iframe src to a variable , when the page initially loads it should show only two buttons on the page , when i click on the button , an iframe should get loaded depending on which button is pressed , i tried doing something like this to create buttons and creating iframes , but it is not working

<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="whatnext">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x = "Hello!";
function showX() {
    var whatnext;
    if b1.onclick==true
        whatnext= "http://munity.sitepoint./" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;"
        elseif b2.onclick=true
        whatnext= "http://munity.sitepoint./" style="border: 0px none; height: 555px; margin-left: -116px; margin-top: -25px; width: 330px;"
}
</script>
and another piece of trial is
<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="x" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x ;
function showX() {
    src="https:http://stackoverflow.";
}
b1.onclick = function() {
    x = "http://stackoverflow.;
    showX();
};
b2.onclick = function() {
    x = "www.sitepoint.";
    showX();
};
</script>
Share asked Oct 8, 2014 at 8:43 beastboybeastboy 1591 gold badge3 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You just need to change the src attribute of the iframe on click. I added the src to the buttons usind the data attribute. Here is the example http://jsfiddle/8wLm42ns/2/ The HTML

    <button class="loadiframe" id="b1" data-src="http://jquery./" data-width="500" data-height="400">Button 1</button>
<button class="loadiframe" id="b2" data-src="http://sitepoint." data-width="650" data-height="350">Button 2</button>

<iframe id='frame' frameborder="0" scrolling="no" width="500" height="400">

jQuery

$('.loadiframe').on('click', function(){
    var src = $(this).data('src'),
        width = $(this).data('width'),
        height = $(this).data('height');
    $('#frame').css({
        width: width,
        height: height
    }).attr('src', src);
});

For javascript solution try this http://jsfiddle/8wLm42ns/3/

The HTML

<div class="loadiframe">
    <button id="b1" data-src="http://jquery./" data-width="500" data-height="400">Button 1</button>
    <button id="b2" data-src="http://sitepoint." data-width="650" data-height="350">Button 2</button>
</div>
<iframe id='frame' frameborder="0" scrolling="no" width="500" height="400">

javaScript - add in head section

function iframeChange() {
    var buttons = document.querySelector(".loadiframe"),
        iframe = document.getElementById('frame');

    buttons.addEventListener("click", function (e) {
        iframe.src = e.target.dataset.src;
        iframe.width = e.target.dataset.width;
        iframe.height = e.target.dataset.height;
    });
}
window.onload = iframeChange;

Try this solution, at click on a button you load the webpage on a specific iframe, no jquery required.

JSBIN here http://jsbin./gebelatomiya/1/

<!doctype html>
<html>
<head>
<script>
function loadFrame (elm){
    var frame1 = document.getElementById('frame1');
    frame1.src = elm.dataset.src;
}
</script>
</head>
<body>
    <button id="b1" data-src="http://www.w3schools." onClick="loadFrame(this)">Button 1</button>
    <button id="b2" data-src="http://www.sony." onClick="loadFrame(this)">Button 2</button>
    <iframe id="frame1" scrolling="no"></iframe>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论